@[SurftheMoon](/user/559529)
```
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
const int N=2001;
typedef pair<int,int> PP;
char g[N][N];
int n;
int x1,y3,x2,y2;
bool st[N][N];
int dist[N][N];
int dx[4]={0,-1,0,1};
int dy[4]={-1,0,1,0};
int bfs(int x1,int y3,int x2,int y2)
{
queue<PP> q;
q.push({x1,y3});
st[x1][y3]=true;
while(!q.empty())
{
PP t=q.front();
q.pop();
int x=t.first,y=t.second;
if(x==x2&&y==y2)return dist[x][y];
for(int i=0;i<4;i++)
{
int a=x+dx[i],b=y+dy[i];
if(a>=1&&a<=1001&&b>=1&&b<=1001)
{
if(st[a][b])continue;
if(g[a][b]=='1')continue;
dist[a][b]=dist[x][y]+1;
st[a][b]=true;
q.push({a,b});
}
}
}
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
cin>>g[i][j];
}
cin>>x1>>y3>>x2>>y2;
cout<<bfs(x1,y3,x2,y2)<<endl;;
}
```
by coldy_rainy @ 2022-02-10 20:41:40
@[SurftheMoon](/user/559529)
错误原因:
以字符串存储的数据第一下标是0
如何改进:
不用字符串存储
不要脸的宣传一下[团队](https://www.luogu.com.cn/team/37776)
by coldy_rainy @ 2022-02-10 20:44:53