Wangyuqi2010 @ 2024-09-29 12:52:54
#include<iostream>
#include<queue>
using namespace std;
// bfs
int step[1050][1050],flag[1050][1050],n,x1,y1,x2,y2;
struct point{
int x;
int y;
};
const int k[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
queue<point>Q;
bool cmp(int x,int y){
if(flag[x][y]==1) return 0;
if(step[x][y]>0) return 0;
if(x<1||x>n) return 0;
if(y<1||y>n) return 0;
return 1;
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cin>>flag[i][j];
}
}
cin>>x1>>y1>>x2>>y2;
Q.push((point){x1,y1});
while(!Q.empty()){
point now=Q.front();
Q.pop();
int nowx=now.x,nowy=now.y;
for(int i=0;i<4;i++){
int nx=nowx+k[i][0];
int ny=nowy+k[i][1];
if(cmp(nx,ny)){
step[nx][ny]=step[nowx][nowy]+1;
Q.push((point){nx,ny});
}
}
}
cout<<step[x2][y2];
return 0;
}
by jntmngm123 @ 2024-09-29 13:04:46
int改成char