TLE,代码:
#include<bits/stdc++.h>
using namespace std;
struct pos
{
int x;
int y;
int step;
};
char a[1010][1010];
int n,i,j,x1,x2,y1,y2;
bool fl=false;
bool flag[1010][1010]={false};
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
queue<pos>q;
int main()
{
cin>>n;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
cin>>a[i][j];
}
}
cin>>x1>>y1;
cin>>x2>>y2;
q.push({x1,y1,1});
flag[x1][y1]=true;
while(!q.empty())
{
if(q.front().x==x2&&q.front().y==y2)
{
cout<<q.front().step-1;
return 0;
}
for(i=0;i<4;i++)
{
int xx=q.front().x+dx[i];
int yy=q.front().y+dy[i];
int st=q.front().step+1;
if(a[xx][yy]=='0'&&((xx>=1&&xx<=n)&&(yy>=1&&yy<=n))&&!flag[xx][yy])
{
q.push({xx,yy,st+1});
flag[xx][yy]=true;
}
}
q.pop();
}
}
/*
3 5
.##.#
.#...
...#.
*/