-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbfs_search.cpp
More file actions
71 lines (56 loc) · 1.44 KB
/
bfs_search.cpp
File metadata and controls
71 lines (56 loc) · 1.44 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <stdio.h>
int n, count;
int map[4][4];
int x[16], y[16], l[16];
void enqueue(int _x, int _y, int _l){
x[count] = _x;
y[count] = _y;
l[count] = _l;
count++;
}
void bfs(int _x, int _y){
int pos = 0;
enqueue(_x, _y, 0); //start position
while(pos < count && (x[pos] != n-1 || y[pos] != n-1)){
//already pass position
map[y[pos]][x[pos]] = 0;
//move up
if(y[pos] > 0 && map[y[pos-1]][x[pos]] == 1){
printf("move up\n");
enqueue(x[pos], y[pos]-1, l[pos]+1);
}
//move down
if(y[pos] < n-1 && map[y[pos]+1][x[pos]] == 1){
printf("move down\n");
enqueue(x[pos], y[pos]+1, l[pos]+1);
}
//move left
if(x[pos] > 0 && map[y[pos]][x[pos]-1] == 1){
printf("move left\n");
enqueue(x[pos]-1, y[pos], l[pos]+1);
}
//move right
if(x[pos] < n-1 && map[y[pos]][x[pos]+1] == 1){
printf("move right\n");
enqueue(x[pos]+1, y[pos], l[pos]+1);
}
//next position
pos++;
}
// printf("pos %d count %d\n", pos, count);
if(pos < count){
printf("Shortest distance: %d\n", l[pos]);
}
}
int main(void){
int i, j;
scanf("%d", &n);
for(i = 0; i < n; i++){
for(j = 0; j < n; j++){
scanf("%d", &map[i][j]);
}
}
printf("\nStart BFS\n\n");
bfs(0,0);
return 0;
}