用广度搜索实现的迷宫算法

#include< iostream>
#include< string>
#include< cstdio>
#include< stack>
#include< queue>
#include< map>
#include<stdlib.h>
#include < algorithm>
#include <math.h>
#include < vector>
using namespace std;
const int maxn=1000;
int A[maxn][maxn];
struct Node{
int x,y;
int step;
int h[maxn];
int z[maxn];
}node;
int n,m;
int X[4]={0,0,1,-1};
int Y[4]={1,-1,0,0};
bool inq[maxn][maxn]={false};
bool judge(int x,int y){
if(x>=n || x<0 || y>=m || y<0)return false;
if(A[x][y]==0 || inq[x][y]==true)return false;
return true;

}
Node s,e;
int BFS(Node s,Node e){
queueq;
node.x=s.x,node.y=s.y;
q.push(node);
inq[s.x][s.y]=true;
while(!q.empty()){
Node top=q.front();
q.pop();
if(top.x == e.x && top.y==e.y)
{
return top.step;
}
for(int i=0;i<4;i++){
int newx=top.x+X[i];
int newy=top.y+Y[i];
if(judge(newx,newy)){

		node.x=newx,node.y=newy;
		node.step=top.step+1;
		node.h[node.step]=node.x;
		node.z[node.step]=node.y;
		q.push(node);
		inq[newx][newy]=true;
	
	
	}
	

}

}

}
int main()
{
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
scanf("%d",&A[i][j]);

	}

}
scanf("%d%d%d%d",&s.x,&s.y,&e.x,&e.y);
node.step=0;
node.h[0]=s.x,node.z[0]=s.y;


int k=BFS(s,e);
printf("%d\n",k);
for(int i=0;i<k;i++){

	printf("(%d,%d)->",node.h[i],node.z[i]);


}
printf("(%d,%d)\n",e.x,e.y);
return 0;

}
在这里插入图片描述
0代表障碍1代表出口

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值