top of page

소스 코드 제출

공개·회원 50명

20250510

/*

#include <stdio.h>

#include <stdlib.h>


int main()

{

printf("Hello world!\n");

return 0;

}

*/


/*

//BFS (QUEUE) .코드


#include <stdio.h>

#include <string.h>

int s, r=0;

int a[101][101]={};

int vt[101]={};

char queue[101]={};

int front=-1, back=-1;


void push(int x)

{

back++;

queue[back]=x;

}


int pop()

{

if(back==front){

return 0;

}

front++;

return queue[front];


}


void bfs(int x)

{

vt[x]=1;

push(x);

while(front!=back){

int p=pop();

for(int i=1; i<=s; i++){

if(a[p][i]==1 && vt[i]!=1){

push(i);

vt[i]=1;

r=r+1;

}

}

}

}


int main()

{

int v, i, j, x, y;

scanf("%d\n%d", &s, &v);

for(i=1; i<=v; i++){

scanf("%d %d", &x, &y);

a[x][y]=1;

a[y][x]=1;

}

bfs(1);

printf("%d", r);

return 0;

}


//DFS코드 ( 재귀)

#include <stdio.h>

int s, r=0;

int a[101][101]={};

int vt[101]={};

void dfs(int x)

{

vt[x]=1;

//노드x와 연결 되어있으면서 , 방문하지 않은 곳으로 가서 dfs를 하세요

for(int i=1; i<=s; i++){

if(a[x][i]==1 && vt[i]==0){

dfs(i);

r=r+1;

}

}

}

int main()

{

int v, i, j, x, y;

scanf("%d\n%d", &s, &v);

for(i=1; i<=v; i++){

scanf("%d %d", &x, &y);

a[x][y]=1;

a[y][x]=1;

}

dfs(1);

printf("%d", r);

return 0;

}


*/


#include <stdio.h>

int s, r=0;

int a[26][26]={};


void dfs(int x, int y) // a[x][y]에서 dfs하세요

{

a[x][y]=0; // 방문했따!


if(a[x+1][y]==1){ // 아래와 연결되어있나요?

dfs(x+1, y); //아래 방문해라!

}

if(a[x-1][y]==1){

dfs(x-1, y);

}

if(a[x][y+1]==1){

dfs(x, y+1);

}

if(a[x][y-1]==1){

dfs(x, y-1);

}

}


int main()

{

int i, j, x, y, cnt=0;

scanf("%d", &s);

for(i=1; i<=s; i++){

for(j=1; j<=s; j++){

scanf("%1d", &a[i][j]);

}

}

for(i=1; i<=s; i++){

for(j=1; j<=s; j++){

if(a[i][j]==1){

dfs(i, j);

cnt++;

}

}

}

printf("%d",cnt);

return 0;

}

3회 조회
주소 : 경기도 용인시 광교중앙로 302 블루 스퀘어 602호
연락처 : 031) 216 - 1546 ,     031) 215 - 1546
사업자등록번호 : 465-92-00916
​학원 등록 제 4603호
bottom of page