#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
using namespace std;
int mi=99999999999;
int xb,yb,xe,ye;
const int maxn=51;
int a[maxn][maxn],book[maxn][maxn];
int move[4][2]={{1,0},{-1,0},{0,1},{0,-1}};//方向数组
struct node{//定义结构体好储存数据
int x,y,s;
}q[maxn*maxn];
int main(){
int i,j,k,m,n,flag;
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++){
for(j=1;j<=m;j++){
scanf("%d",&a[i][j]);//存图
}
}
scanf("%d%d%d%d",&xb,&yb,&xe,&ye);
int h=1,t=1;
q[h].x=xb;
q[h].y=yb;
q[h].s=0;
t++;//入队
book[xb][yb]=1;
while(h<t){//广搜
for(k=0;k<=3;k++){
int tx=q[h].x+move[k][0];
int ty=q[h].y+move[k][1];
if(tx<1||ty>m||ty<1||tx>n)continue;
if(a[tx][ty]==0&&book[tx][ty]==0){
book[tx][ty]=1;
q[t].x=tx;
q[t].y=ty;
q[t].s=q[h].s+1;
t++;
}
if(tx==xe&&ty==ye){
flag=1;
break;
}
}
if(flag)break;
h++;//出队
}
printf("%d",q[t-1].s);
return 0;
}
bfs解救小哈
最新推荐文章于 2022-03-01 14:34:08 发布