mot1ve @ 2020-04-07 15:07:20
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
int n;
int xx[4]={0,0,1,-1};
int yy[4]={1,-1,0,0};
int dis[1010][1010],vis[1010][1010];
char f[1010][1010];
struct node{
int a,b;
};
queue<node> q;
int x1,y1,fx,fy,sx,sy;
int bfs(int x,int y)
{
q.push((node){x,y});
vis[x][y]=1;
while(!q.empty());
{
x1=q.front().a;
y1=q.front().b;
q.pop();
if(x1==fx&&y1==fy)
{
return dis[x1][y1];
}
for(int i=0;i<4;i++)
{
int dx=x1+xx[i];
int dy=y1+yy[i];
if(dx>0&&dx<=n&&dy>0&&dy<=n&&vis[dx][dy]==0&&f[dx][dy]=='0')
{
vis[dx][dy]=1;
dis[dx][dy]=dis[x1][y1]+1;
q.push((node){dx,dy});
}
}
}
}
char d;
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
cin>>f[i][j];
}
}
cin>>sx>>sy>>fx>>fy;
cout<<bfs(sx,sy);
return 0;
}
by mot1ve @ 2020-04-07 15:39:36
@森蚺 bfs不需要回溯
by mot1ve @ 2020-04-07 15:39:58
@森蚺 您改一下可以吗
by Ckger @ 2020-04-07 15:52:09
@kuoluo03 额,我刚看错了,但我好像知道您哪里错了
by Ckger @ 2020-04-07 15:52:53
@kuoluo03 您是不是while(!q.empty())后面多打了一个分号所以错了
by mot1ve @ 2020-04-07 17:46:10
@森蚺 谢谢,,我这才发现 居然没报错