//BFS 自己推一遍
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
int N,M; //迷宫边界
int xo,yo; //起点坐标
int xn,yn; //终点坐标
int region[1000][1000];//迷宫
int book[1000][1000]; //判断是否走过
int place[4][2]={0,1,1,0,0,-1,-1,0};
int Min=0;
struct node
{
int x;
int y;
int s;
}st;
queue<node> q;
bool dfs(node v0)
{
node v1;
int tx,ty;
while(!q.empty())
{
v1=q.front();
q.pop();
for(int i=0;i<=3;i++)
{
tx=v1.x+place[i][0];
ty=v1.y+place[i][1];
if(tx>=0&&tx<N&&ty>=0&&ty<M&&book[tx][ty]==0&®ion[tx][ty]==0)//判断是否满足条件
{
book[tx][ty]=1;
st.x=tx;
st.y=ty;
st.s=v1.s+1;
q.push(st);
}
if(tx==xn&&ty==yn)
{
return true;
}
}
}
return false;
}
int main()
{
node v2;
cin>>N>>M;
for(int i=0;i<N;i++)
for(int j=0;j<M;j++)
cin>>region[i][j];
cin>>xo>>yo>>xn>>yn;
book[xo][yo]=1;
st.x=xo,st.y=yo,st.s=0;
q.push(st);
if(dfs(st))
{
v2=q.back();
cout<<v2.s<<endl;
}
return 0;
}
//基本理解
BFS
最新推荐文章于 2023-09-28 12:18:32 发布