```cpp
#include<iostream>
#include<queue>
using namespace std;
struct Pos
{
int x,y;
};
queue <Pos> q;
int n,x,y,tx,ty,dis[1001][1001],s_a,s_b,t_a,t_b;
const int dx[]={1,-1,0,0};
const int dy[]={0,0,1,-1};
char mp[1001][1001];
bool vis[1001][1001];
int bfs(int sx,int sy)
{
q.push((Pos){sx,sy});
vis[sx][sy]=true;
while(!q.empty())
{
x=q.front().x;
y=q.front().y;
q.pop();
if(x==t_a&&y==t_b) return dis[x][y];
for(int i=0;i<4;i++)
{
tx=x+dx[i];
ty=y+dy[i];
if(tx<=0||tx>n||ty<=0||ty>n) continue;
if(mp[tx][ty]=='1'||vis[tx][ty]==true) continue;
dis[tx][ty]=dis[x][y]+1;
vis[tx][ty]=true;
q.push((Pos){tx,ty});
}
}
return -1;
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
cin>>mp[i][j];
cin>>s_a>>s_b>>t_a>>t_b;
cout<<bfs(s_a,s_b);
return 0;
}
```
by _yjh @ 2020-01-16 17:30:03
@[lucy2008](/user/95170) 我的代码
by _yjh @ 2020-01-16 17:30:18
没有加vis标记,
by zfz04 @ 2020-01-16 17:34:37
我也爆空间
by goodkillerchen @ 2020-04-21 14:21:25