题目
#include<bits/stdc++.h>
using namespace std;
int a[1100][1100];
int b[110000][3];
bool op[1010][1010];
int qwe[1010][1010];
int fx[5]={0,0,1,0,-1};
int fy[5]={0,1,0,-1,0};
int n,tx,ty;
int xa,ya,xb,yb;
int head=1,rear=1;
int main(){
cin>>n;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
char s;
cin>>s;
a[i][j]=s-'0';
}
}
cin>>xa>>ya>>xb>>yb;
b[1][1]=xa;
b[1][2]=ya;
op[xa][ya]=true;
while(head<=rear){
for(int i=1;i<=4;i++){
tx=b[head][1]+fx[i];
ty=b[head][2]+fy[i];
if(tx>=1&&tx<=n&&ty>=1&&ty<=n&&a[tx][ty]!=1&&op[tx][ty]==false){
rear++;
qwe[tx][ty]=qwe[b[head][1]][b[head][2]]+1;
op[tx][ty]=true;
b[rear][1]=tx;
b[rear][2]=ty;
}
}
head++;
}
cout<<qwe[xb][yb];
return 0;
}
评测记录