#include<stdio.h>
int n,m,p,q,min = 99999999;
int a[51][51] = {0},book[51][51] = {0};
int head,tail;
struct node{
int x;
int y;
int s;
};
struct node queu[2501];
void bfs(int x,int y){
printf("(%d,%d):\n",x,y);
printf("queu[tail - 1].s = %d\n",queu[tail - 1].s);
int next[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};
if(x == p && y == q){
if(queu[head].s < min){
min = queu[tail - 1].s;
}
return;
}
int tx,ty;
int i;
for(i = 0;i <= 3;i ++){
tx = x + next[i][0];
ty = y + next[i][1];
if(tx < 1 || tx > n || ty < 1 || ty > m){
continue;
}
if(book[tx][ty] == 0 && a[tx][ty] == 0){
queu[tail].x = tx;
queu[tail].y = ty;
queu[tail].s = queu[head].s + 1;
tail ++;
book[tx][ty] = 1;
}
}
head ++;
bfs(queu[head].x,queu[head].y);
return;
}
int main(){
int i,j;
int startx,starty;
head = 1;
tail = 1;
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);
book[startx][starty] = 1;
queu[tail].x = startx;
queu[tail].y = starty;
queu[tail].s = 0;
tail ++;
bfs(startx,starty);
//printf("%d\n",queu[tail - 1].s);
/*for(i = head;i < tail;i ++){
printf(" queu.s :%d ",queu[i].s);
}*/
printf("\n");
printf("%d\n",min);
}
//logic
这次用了之前总结的经验果然得到了很大的解决,只有
if(book[tx][ty] == 0 && a[tx][ty] == 0)
写成了
if(book[tx][ty] == 0 || a[tx][ty] == 0)
后来printf输出结果的方法才找对了问题的关键所在。
但是我发现自己老早之前试验了几个很近的点(给的标准输入和输出的改动下)都是对的,我就觉的是把墙算进去了,因为按照数学归纳法的启示,这应该像多米诺骨牌一样证明正确性的,毕竟直接判断对错都是正确的,唯一特殊的可能就是比较近的地方没有墙壁。
错误——
5 4
0 0 1 0
0 0 0 0
0 0 1 0
0 1 0 0
0 0 0 1
1 1 4 3
(1,1):
queu[tail - 1].s = 0
(1,2):
queu[tail - 1].s = 1
(2,1):
queu[tail - 1].s = 2
(1,3):
queu[tail - 1].s = 2
(2,2):
queu[tail - 1].s = 3
(1,1):
queu[tail - 1].s = 3
(2,2):
queu[tail - 1].s = 3
(3,1):
queu[tail - 1].s = 3
(1,1):
queu[tail - 1].s = 3
(1,4):
queu[tail - 1].s = 3
(2,3):
queu[tail - 1].s = 4
(1,2):
queu[tail - 1].s = 4
(2,3):
queu[tail - 1].s = 4
(3,2):
queu[tail - 1].s = 4
(2,1):
queu[tail - 1].s = 4
(1,2):
queu[tail - 1].s = 4
(1,2):
queu[tail - 1].s = 4
(2,1):
queu[tail - 1].s = 4
(2,3):
queu[tail - 1].s = 4
(3,2):
queu[tail - 1].s = 4
(2,1):
queu[tail - 1].s = 4
(1,2):
queu[tail - 1].s = 4
(3,2):
queu[tail - 1].s = 4
(4,1):
queu[tail - 1].s = 4
(2,1):
queu[tail - 1].s = 4
(1,2):
queu[tail - 1].s = 4
(2,1):
queu[tail - 1].s = 4
(2,4):
queu[tail - 1].s = 4
(2,4):
queu[tail - 1].s = 5
(3,3):
queu[tail - 1].s = 5
(2,2):
queu[tail - 1].s = 5
(2,2):
queu[tail - 1].s = 5
(1,1):
queu[tail - 1].s = 5
(2,4):
queu[tail - 1].s = 5
(2,2):
queu[tail - 1].s = 5
(4,2):
queu[tail - 1].s = 5
(3,1):
queu[tail - 1].s = 5
(2,2):
queu[tail - 1].s = 5
(2,2):
queu[tail - 1].s = 5
(3,1):
queu[tail - 1].s = 5
(1,1):
queu[tail - 1].s = 5
(2,2):
queu[tail - 1].s = 5
(1,1):
queu[tail - 1].s = 5
(2,2):
queu[tail - 1].s = 5
(1,1):
queu[tail - 1].s = 5
(2,2):
queu[tail - 1].s = 5
(3,1):
queu[tail - 1].s = 5
(1,1):
queu[tail - 1].s = 5
(2,4):
queu[tail - 1].s = 5
(2,2):
queu[tail - 1].s = 5
(3,1):
queu[tail - 1].s = 5
(2,2):
queu[tail - 1].s = 5
(2,2):
queu[tail - 1].s = 5
(3,1):
queu[tail - 1].s = 5
(1,1):
queu[tail - 1].s = 5
(2,2):
queu[tail - 1].s = 5
(1,1):
queu[tail - 1].s = 5
(3,1):
queu[tail - 1].s = 5
(2,2):
queu[tail - 1].s = 5
(5,1):
queu[tail - 1].s = 5
(3,1):
queu[tail - 1].s = 5
(2,2):
queu[tail - 1].s = 5
(3,1):
queu[tail - 1].s = 5
(1,1):
queu[tail - 1].s = 5
(2,2):
queu[tail - 1].s = 5
(1,1):
queu[tail - 1].s = 5
(2,2):
queu[tail - 1].s = 5
(3,1):
queu[tail - 1].s = 5
(1,1):
queu[tail - 1].s = 5
(3,4):
queu[tail - 1].s = 5
(2,3):
queu[tail - 1].s = 6
(1,4):
queu[tail - 1].s = 6
(3,4):
queu[tail - 1].s = 6
(2,3):
queu[tail - 1].s = 6
(1,4):
queu[tail - 1].s = 6
(3,4):
queu[tail - 1].s = 6
(4,3):
queu[tail - 1].s = 6
queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :5 queu.s :6 queu.s :6 queu.s :6 queu.s :6 queu.s :6 queu.s :6 queu.s :6 queu.s :6 queu.s :6 queu.s :6 queu.s :6 queu.s :6
6
还有这提示我即使前面几个是对的最后答案是错的也有可能是从一开始就是错的
正确——
5 4
0 0 1 0
0 0 0 0
0 0 1 0
0 1 0 0
0 0 0 1
1 1 4 3
(1,1):
queu[tail - 1].s = 0
(1,2):
queu[tail - 1].s = 1
(2,1):
queu[tail - 1].s = 2
(2,2):
queu[tail - 1].s = 2
(3,1):
queu[tail - 1].s = 3
(2,3):
queu[tail - 1].s = 3
(3,2):
queu[tail - 1].s = 4
(4,1):
queu[tail - 1].s = 4
(2,4):
queu[tail - 1].s = 4
(5,1):
queu[tail - 1].s = 5
(3,4):
queu[tail - 1].s = 5
(1,4):
queu[tail - 1].s = 6
(5,2):
queu[tail - 1].s = 6
(4,4):
queu[tail - 1].s = 6
(5,3):
queu[tail - 1].s = 7
(4,3):
queu[tail - 1].s = 7