迷宫问题BFS

相信大家在学习数据结构时都会遇到一个问题,即迷宫问题。一般来说大家都会像真实走迷宫一样进行DFS搜索,但我今天给出的是随机生成迷宫且找最短路径的代码。通过这一次的编程,我发现自己要注意以下两点:1.要注意程序运行过程中的变量改变;2.对于复杂的条件判断,需要细心。

Have fun coding,i_human.Have fun coding,everyone!

THE CODE:


// 数据结构迷宫.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <cstdlib>
#include<iostream>
#include<queue>
#include<ctime>
#define N 4  //定义迷宫为4*4

using namespace std;

struct pot  //为记录路径的rec[]准备,坐标(x,y)
{
	int x;
	int y;
	inline bool operator ==(const pot a)
	{
	return x==a.x && y==a.y;
	}
};

class maze
{
public:
	maze();
	bool find();
	void print();
	void path();
	~maze(){};
private:
	void create();
	void clearrec();
	int pic[N+2][N+2];  //迷宫本身
	pot rec[N+1][N+1];  //BFS过程中记录路径
};

maze::maze()
{
	create();
	clearrec();
}

bool maze::find()  //使用BFS寻求最短路径
{
	pot n;n.x=n.y=0;
	queue<pot>qu;
	pot tmp;tmp.x=tmp.y=1;
	qu.push(tmp);
	while(!qu.empty())
	{
		if(pic[qu.front().x][qu.front().y-1]!=1 && rec[qu.front().x][qu.front().y-1]==n)
		{
			tmp.x=qu.front().x;tmp.y=qu.front().y-1;
			qu.push(tmp);
			tmp.y++;
			rec[qu.front().x][qu.front().y-1]=tmp;
			if(pic[qu.front().x][qu.front().y-1]==3)
				return true;
		}
		if(pic[qu.front().x+1][qu.front().y]!=1 && rec[qu.front().x+1][qu.front().y]==n)
		{
			tmp.x=qu.front().x+1;tmp.y=qu.front().y;
			qu.push(tmp);
			tmp.x--;
			rec[qu.front().x+1][qu.front().y]=tmp;
			if(pic[qu.front().x+1][qu.front().y]==3)
				return true;
		}
		if(pic[qu.front().x][qu.front().y+1]!=1 && rec[qu.front().x][qu.front().y+1]==n)
		{
			tmp.x=qu.front().x;tmp.y=qu.front().y+1;
			qu.push(tmp);
			tmp.y--;
			rec[qu.front().x][qu.front().y+1]=tmp;
			if(pic[qu.front().x][qu.front().y+1]==3)
				return true;
		}
		if(pic[qu.front().x-1][qu.front().y]!=1 && rec[qu.front().x-1][qu.front().y]==n)
		{
			tmp.x=qu.front().x-1;tmp.y=qu.front().y;
			qu.push(tmp);
			tmp.x++;
			rec[qu.front().x-1][qu.front().y]=tmp;
			if(pic[qu.front().x-1][qu.front().y]==3)
				return true;
		}
		qu.pop();
	}
	return false;
}

void maze::path()
{
	int a=N,b=N;
	while(pic[a][b]!=2)
	{
		cout<<"("<<a<<","<<b<<")"<<"<--";
		int c=a;
		a=rec[a][b].x;
		b=rec[c][b].y;
	}
	cout<<"(1,1)"<<endl;
}

void maze::print()  //打印出随机生成的迷宫
{
	for(int i=0;i<N+2;i++)
	{
		for(int j=0;j<N+2;j++)
			cout<<pic[i][j]<<" ";
		cout<<endl;
	}
}

void maze::create()  //随机生成迷宫,每次以左上为起点,右下为终点
{
	for(int i=0;i<N+2;i++)
		pic[0][i]=pic[N+1][i]=1;
	for(int i=1;i<N+1;i++)
		pic[i][0]=pic[i][N+1]=1;
	srand((unsigned)time(NULL));
	for(int i=1;i<N+1;i++)
		for(int j=1;j<N+1;j++)
			pic[i][j]=rand()%2;
	pic[1][1]=2;
	pic[N][N]=3;
}

void maze::clearrec() //初始化路径记录器
{
	for(int i=1;i<=N;i++)
		for(int j=1;j<=N;j++)
			rec[i][j].x=rec[i][j].y=0;
	rec[1][1].x=rec[1][1].y=N;  //防止回到起点的无用操作
}

int main()
{
	maze example;
	example.print(); 
	if(example.find())
		example.path();
	else
		cout<<"the solution can not be found."<<endl;
	system("pause");
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值