Hiraeth @ 2019-01-11 12:26:02
#include<bits/stdc++.h>
using namespace std;
int f[1005][1005],used[1005][1005],n,nx,ny,mx,my,px,py,tmpx,tmpy;
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
char a;
queue <pair<int,int> > q;
void bfs(){
while (!q.empty()){
px=q.front().first;
py=q.front().second;
q.pop();
for (int i=0;i<3;i++){
tmpx=px+dx[i];
tmpy=py+dy[i];
if (tmpx>0&&tmpx<=n&&tmpy>0&&tmpy<=n&&f[tmpx][tmpy]==0&&used[tmpx][tmpy]==0){
used[tmpx][tmpy]=1;
f[tmpx][tmpy]=f[px][py]+1;
if (tmpx==mx&&tmpy==my){
printf("%d\n",f[tmpx][tmpy]);
exit(0);
}
q.push(make_pair(tmpx,tmpy));
}
}
}
}
int main(){
scanf("%d",&n);
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++){
cin>>a;
f[i][j]=a-'0';
}
scanf("%d%d%d%d",&nx,&ny,&mx,&my);
q.push(make_pair(nx,ny));
used[nx][ny]=1;
bfs();
return 0;
}
by BCZSX @ 2019-01-11 12:53:05
你这个方向数组要从0枚举到3,而不是到2,他要寻找最短路,因为有许多条路,所以每次走到后都必须判断一次,找到最短的那一条,所以说……你的bfs函数要改一下@Hiraeth
by kqing @ 2019-02-01 11:11:32
for (int i=0;i<4;i++)