LightBit @ 2019-05-30 21:40:35
#include<bits/stdc++.h>
using namespace std;
int p[1051][1051],n,x,y,fx,fy;
int xg[4]={0,0,1,-1};
int yg[4]={1,-1,0,0};
struct queue_s{
int x,y;
int s;
}que_s[1150001];
void bfs(){
int head=0,tail=1,i;
que_s[1].x=x;que_s[1].y=y;que_s[1].s=0;
while(head<=tail){
head++;
while(que_s[head].x==0||que_s[head].y==0){
head++;
if(head>tail) return ;
}
int h1=que_s[head].x,h2=que_s[head].y;
for(i=0;i<4;i++){
int jd1=h1+xg[i],jd2=h2+yg[i];
if(!p[jd1][jd2]&&jd1>0&&jd2>0&&jd1<=n&&jd2<=n){
p[jd1][jd2]=1;
tail++;
que_s[tail].x=jd1;
que_s[tail].y=jd2;
que_s[tail].s=que_s[head].s+1;
if(jd1==fx&&jd2==fy){
cout<<que_s[tail].s;
head=INT_MAX;tail=0;
}
}
}
}
}
int main(){
cin>>n;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
scanf("%1d",&p[i][j]);
cin>>x>>y>>fx>>fy;
p[x][y]=1;
bfs();
return 0;
}