-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbfs.cpp
More file actions
53 lines (37 loc) · 847 Bytes
/
bfs.cpp
File metadata and controls
53 lines (37 loc) · 847 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <stdio.h>
int n;
int rear, front;
int map[4][4], queue[4], visit[4];
void bfs(int v){
int i;
visit[v] = 1;
printf("Start at %d\n", v);
printf("rear %d\n", rear);
printf("front %d\n", front);
queue[rear++] = v;
while(front < rear){
v = queue[front++];
for(i = 1; i <= n; i++){
if(map[v][i] == 1 && !visit[i]){
visit[i] = 1;
printf("move %d to %d\n", v, i);
queue[rear++] = i;
}
}
}
}
int main(void){
int start;
int v1, v2;
scanf("%d%d", &n, &start);
while(1){
scanf("%d%d", &v1, &v2);
if(v1 == -1 && v2 == -1){
break;
}
map[v1][v2] = map[v2][v1] = 1;
}
printf("BFS Start!!\n\n");
bfs(start);
return 0;
}