最靓的哥 @ 2022-04-20 20:47:43
#include<iostream>
#include<queue>
using namespace std;
int n,x1,y1,x2,y2,s[1005][1005];
bool a[1005][1005];
char str[1005];
queue<int> qx,qy;
int d[4][2]={{-1,0},{1,0},{0,1},{0,-1}};
void bfs(){
qx.push(x1);
qy.push(x2);
s[x1][x2]=0;
a[x1][x2]=1;
while(!qx.empty()&&!qy.empty()){
for(int i=0;i<4;i++){
int nx=qx.front()+d[i][0],ny=qy.front()+d[i][1];
if(nx>0&&nx<=n&&ny>0&&ny<=n&&a[nx][ny]==0){
a[nx][ny]=1;
s[nx][ny]=s[qx.front()][qy.front()]+1;
if(nx==x2&&ny==y2){
cout<<s[nx][ny];
return;
}
qx.push(nx);
qy.push(ny);
}
}
qx.pop();
qy.pop();
}
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>(str+1);
for(int j=1;j<=n;j++){
if(str[j]=='1')a[i][j]=1;
}
}
cin>>x1>>y1>>x2>>y2;
bfs();
return 0;
}
by 最靓的哥 @ 2022-04-20 20:49:10
只能过样例……
by syta @ 2022-04-21 11:27:19
bfs函数里面:
by syta @ 2022-04-21 11:29:04
昂,还有s[x1][y1]=0;
by 最靓的哥 @ 2022-04-21 21:46:51
谢谢大犇!!!
by 最靓的哥 @ 2022-04-21 21:47:45
@syta,你是不是孙雨彤啊!?
by 最靓的哥 @ 2022-04-21 21:48:02
好眼熟啊
by 最靓的哥 @ 2022-04-21 22:04:40
谢谢啦
by syta @ 2022-04-23 19:58:49
@最靓的哥 是的,我只是改了个名而已