-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbfsSaveHA.c
More file actions
79 lines (63 loc) · 1.74 KB
/
Copy pathbfsSaveHA.c
File metadata and controls
79 lines (63 loc) · 1.74 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
72
73
74
75
76
77
78
79
#include <stdio.h>
struct note
{
int x; int y;
int f; int s;
};
int main()
{
struct note que[2501];
int a[51][51] = {0}, book[51][51]={0};
int next[4][2] = {{1,0},{0,-1},{-1,0},{0,1}};
int head=1,tail=1;
int i,j,k,n,m,startx,starty,p,q,tx,ty,flag;
scanf("%d %d", &n, &m);
for(i=1; i<=n; i++)
for(j=1; j<=m; j++)
scanf("%d", &a[i][j]);
scanf("%d %d %d %d", &startx, &starty, &p, &q);
que[tail].x = startx;
que[tail].y = starty;
que[tail].f = 0;
que[tail].s = 0;
tail++;
// printf("%d", tail);
book[startx][starty] = 1;
flag = 0;
while(head<tail)
{
for(k=0; k<4; k++)
{
// 注意检查变量名称和变量声明位置
tx = que[head].x+next[k][0];
ty = que[head].y+next[k][1];
if(tx<1 || tx>n || ty<1 || ty>m)
continue;
if(book[tx][ty] == 0 && a[tx][ty] == 0)
{
book[tx][ty] = 1;
que[tail].x = tx;
que[tail].y = ty;
que[tail].f = head;
que[tail].s = que[head].s+1;
tail++;
// head++;
}
if(tx == p && ty == q)
{
// if(que[tail-1].s < min)
// min = que[tail-1];
// 广度优先BFS是不需要储存最小步数的
flag = 1;
break;
}
}
if(flag == 1)
break;
head++;
}
printf("Min of step is %d", que[tail-1].s);
// printf("%d %d", que[tail-1].x, que[tail-1].y);
getchar();getchar();
return 0;
}