tengyuxuan @ 2025-01-10 19:35:26
#include <bits/stdc++.h>
using namespace std;
int n;
int x2,y2;
int fx[]={0,1,-1,0};
int fy[]={1,0,0,-1};
char a[1003][1003];
struct node{
int x;
int y;
int step;
};
queue<node> q;
bool vis[1003][1003];
void bfs(int x1,int y1)
{
q.push({1,1,0});
vis[1][1]=1;
while(!q.empty()){
node fir=q.front();
for(int i=0;i<4;i++)
{
int dx=fir.x+fx[i];
int dy=fir.y+fy[i];
if(dx==n&&dy==n)
{
cout<<fir.step+1;
exit(0);
}
if(dx>=x1&&dx<=n&&dy>=y1&&dy<=n&&a[dx][dy]=='0'&&!vis[dx][dy])
{
vis[dx][dy]=1;
q.push({dx,dy,fir.step+1});
}
}
q.pop();
}
}
int main()
{
int x1,y1;
ios::sync_with_stdio(0);
cin>>n;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)cin>>a[i][j];
}
cin>>x1>>y1>>x2>>y2;
bfs(x1,y1);
}