风声键客 @ 2021-01-25 21:04:23
#include<bits/stdc++.h>
using namespace std;
const int a=1005,I=100000000;
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
char field[a][a];
int n;
int bf[a][a];
typedef pair<int,int> P;
int bfs(int x1,int y1,int x2,int y2){
queue<P> que;
for(int i=0;i<=a;i++){
for(int j=0;j<=a;j++){
bf[i][j]=I;
}
}que.push(P(x1,y1));
bf[x1][y1]=0;
while(que.size()){
P p=que.front();
que.pop();
if(p.first==x2&&p.second==y2) break;
for(int i=0;i<=3;i++){
int nx=p.first+dx[i],ny=p.second+dy[i];
if(nx>0&&nx<=n&&ny>0&&ny<=n&&field[nx][ny]=='0'&&bf[nx][ny]==I){
que.push(P(nx,ny));
bf[nx][ny]=bf[p.first][p.second]+1;
}
}
}return bf[x2][y2];
}
int main(){
int x1,y1,x2,y2;
cin>>n;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cin>>field[j][i];
}
}cin>>x1>>y1>>x2>>y2;
cout<<bfs(x1,y1,x2,y2)<<endl;
}
by Scintilla @ 2021-01-25 21:42:54
变量名不要用 x1 x2 y1 y2
之类,头文件里好像已经用过了