qinyi2022 @ 2022-07-23 10:56:22
#include<bits/stdc++.h>
using namespace std;
int dx[4]={-1,1,0,0};
int dy[4]={0,0,-1,1};
int n,qx,qy,ex,ey;
char a[1001][1001];
int c[1001][3];
void f(int x,int y){
int t=1,h=0;
a[x][y]='1';
c[t][0]=x;
c[t][1]=y;
c[t][2]=0;
while(h!=t){
h++;
for(int i=0;i<4;i++){
int p=c[h][0]+dx[i];
int q=c[h][1]+dy[i];
if(p==ex&&q==ey){
cout<<c[h][2]+1;
return ;
}
if(p<=n&&p>=1&&q<=n&&q>=1&&a[p][q]=='0'){
t++;
a[p][q]='1';
c[t][0]=p;
c[t][1]=q;
c[t][2]=c[h][2]+1;
}
}
}
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cin>>a[i][j];
}}
cin>>qx>>qy>>ex>>ey;
f(qx,qy);
return 0;
}
by FstAutoMaton @ 2022-10-04 11:49:58
你这代码思路好乱啊,这道题还是要用bfs 做
by FstAutoMaton @ 2022-10-04 11:50:28
@qinyi2022