广搜,挨个遍历,记录起点和终点,下一个的起点等于终点+1
#include<bits/stdc++.h>
using namespace std;
int xx[4]={0,0,1,-1},yy[4]={1,-1,0,0};
int kt,s=0,kts,ktts;
char c;
int i,j,n,a[1200][1200],x1,x2,y2,ktt,x[1100000],y[1100000];
int main(){
cin>>n;
for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
cin>>c;
a[i][j]=c-'0';
}
}
cin>>x[1]>>y[1];
cin>>x2>>y2;
kt=1;
ktt=1;
while(1){
s++;
kts=kt;
ktts=ktt;
for(i=ktts;i<=kts;i++){
for(j=0;j<4;j++){
int xxx=x[i]+xx[j];
int yyy=y[i]+yy[j];
if(xxx==x2&&yyy==y2){
cout<<s;
return 0;
}
if(xxx<=n&&xxx>=1&&yyy>=1&&yyy<=n&&a[xxx][yyy]==0){
kt++;
a[xxx][yyy]=1;
x[kt]=xxx;
y[kt]=yyy;
}
}
}
ktt=kts+1;
}
}