-
백준1260_DFS와 BFS알고리즘/백준 2020. 6. 3. 20:11
https://www.acmicpc.net/problem/1260
#include <stdio.h> #define MAX 1001 int graph[MAX][MAX]={0,}; int bfs[MAX]={0,}; int dfs[MAX]={0,}; void DFS(int N, int s); void BFS(int N, int s); int main(){ int N,M,V; scanf("%d %d %d",&N,&M,&V); int n; int i,a,b; for(i=0; i<M; i++){ scanf("%d",&a); scanf("%d",&b); graph[a][b]=1; graph[b][a]=1; } DFS(N,V); putchar('\n'); BFS(N,V); return 0; } void DFS(int N, int s){ int i; printf("%d ",s); dfs[s]=1; for(i=1; i<=N; i++){ if(graph[s][i]==1 && dfs[i]!=1) DFS(N,i); } } void BFS(int N, int s){ int i; int front = -1; int rear = -1; int queue[10001]={0,}; int pop; rear++; queue[rear] = s; bfs[s]=1; printf("%d ", s); while (front < rear) { front++; pop = queue[front]; for (i=1; i <= N; i++) { if (graph[pop][i] == 1 && bfs[i] == 0) { rear++; queue[rear] = i; bfs[i] = 1; printf("%d ", i); } } } }
'알고리즘 > 백준' 카테고리의 다른 글
백준 1110_더하기 사이클 (0) 2020.10.09 백준 2193_이친수 (0) 2020.08.17 백준 1463_1로 만들기 (0) 2020.08.17 Dynamic Programming (0) 2020.08.09 백준 알고리즘2798_블랙잭 (0) 2020.05.28