广度优先搜索算法解迷宫问题

前面有使用深度优先算法来解迷宫问题,思路是使用递归的方法,先进行深度的尝试,找到或者无法抵达时返回上一级函数继续执行。

本文使用广度优先算法,思路如下:如图在(1,1)的位置开始,广度优先算法的步骤是搜索一步能够抵达的所有位置作为第一层,然后在上一层的基础上再走一步,并且不走重复的格子,作为下一层,这样一层一层的搜索,达到终点再停止。

下面程序用广度搜索算法解决迷宫的问题,数据结构十分巧妙,同样可以获得12的结果。

#include <stdio.h>
using namespace std;

struct Position
{
	Position()
	{
		x = 0;
		y = 0;
		father = 0;
		step = 0;

	}
	Position(int row, int col)
	{
		x = row;
		y = col;
		father = 0;
		step = 0;
	}
	int x;//横坐标
	int y;//纵坐标
	int father;//父节点在队列中的编号
	int step;//步数
	Position& operator = (Position& p)
	{
		x = p.x;
		y = p.y;
		return *this;
	}
	bool operator == (Position& p)
	{
		return x == p.x && y == p.y;
	}
};

enum Direction
{
	Left,
	Right,
	Up,
	Down
};

Position NextPosition(Position pNow, Direction dMove)
{
	Position pTarget = pNow;
	switch (dMove)
	{
	case Left:
		pTarget.y--;
		break;
	case Right:
		pTarget.y++;
		break;
	case Up:
		pTarget.x--;
		break;
	case Down:
		pTarget.x++;
		break;
	default:
		break;
	}
	return pTarget;
}

const int ROW = 5;
const int COL = 4;
int MazeMap[ROW][COL] = {
	0,0,1,0,
	0,0,1,0,
	0,0,1,0,
	0,1,0,0,
	0,0,0,1
};
bool book[ROW][COL] = { false };

//广度优先算法
void bfs(Position p, int& iTotalStep)
{
	Position pQueue[ROW*COL];
	Position pEnd(0,3);
	int head = 0;
	int tail = 0;
	pQueue[head].x = p.x;
	pQueue[head].y = p.y;
	pQueue[head].father = head;
	pQueue[head].step = 0;
	book[p.x][p.y] = true;
	tail++;
	while (head < tail)
	{
		bool bFindTarget = false;
		for (int i=0; i<=Down; i++)
		{
			Position pTarget = NextPosition(pQueue[head], (Direction)i);
			if (pTarget.x <0 || pTarget.y <0 || pTarget.x >= ROW || pTarget.y >= COL)
			{
				continue;
			}
			if (!book[pTarget.x][pTarget.y] && MazeMap[pTarget.x][pTarget.y] == 0)
			{
				book[pTarget.x][pTarget.y] = true;
				pQueue[tail].x = pTarget.x;
				pQueue[tail].y = pTarget.y;
				pQueue[tail].father = head;
				pQueue[tail].step = pQueue[head].step + 1;
				tail++;
			}

			if (pTarget == pEnd)
			{
				bFindTarget = true;
				iTotalStep = pQueue[head].step + 1;
				break;
			}

		}
		if (bFindTarget)
		{
			break;
		}
		head++;		
	}
}


int main()
{
	Position pStart(0,1);
	int iSetp = 0;
	bfs(pStart, iSetp);

    printf("Shortest path length is %d.", iSetp);
}

 

好的,我可以为您讲C++使用广度优先搜索算法解决迷宫问题广度优先搜索算法是一种用于遍历或搜索树或图的算法,它从起点开始,逐层遍历每个可能的路径,直到找到目标节点或无法继续搜索为止。 在解决迷宫问题时,我们可以将迷宫看作一个图,其中每个格子都是一个节点,相邻的格子之间有一条边。我们可以使用广度优先搜索算法从起点开始遍历图,直到找到终点为止。在搜索过程中,我们需要记录每个节点的深度,并且每次遍历到一个节点时,都需要判断它是否是终点,如果是终点则搜索结束,否则继续向下搜索。 以下是C++代码示例: ```c++ #include <iostream> #include <queue> //使用STL库中的队列 #define ROW 5 #define COL 5 using namespace std; struct Node{ int x, y, depth; }; int maze[ROW][COL] = { {0, 0, 0, 0, 0}, {0, 1, 1, 0, 0}, {0, 1, 1, 0, 0}, {0, 0, 0, 1, 0}, {0, 0, 0, 1, 0} }; int visited[ROW][COL] = {0}; //记录节点是否被访问过 int dx[4] = {0, 0, 1, -1}; //定义四个方向的移动 int dy[4] = {1, -1, 0, 0}; void bfs(int x, int y){ queue<Node> q; //定义队列 Node start = {x, y, 0}; //起点节点 q.push(start); //将起点节点入队 visited[x][y] = 1; //标记起点节点已访问 while(!q.empty()){ //队列不为空时循环 Node cur = q.front(); //取出队首元素 q.pop(); //将队首元素出队 if(cur.x == ROW-1 && cur.y == COL-1){ //到达终点 cout << "Find the way! The shortest path length is " << cur.depth << endl; return; } for(int i=0; i<4; ++i){ //向四个方向进行搜索 int nx = cur.x + dx[i]; int ny = cur.y + dy[i]; if(nx<0 || nx>=ROW || ny<0 || ny>=COL) continue; //越界跳过 if(visited[nx][ny] || maze[nx][ny]) continue; //已访问或者是墙跳过 visited[nx][ny] = 1; //标记为已访问 Node next = {nx, ny, cur.depth+1}; //将下一个节点入队 q.push(next); } } cout << "Can't find the way!" << endl; //无法到达终点 } int main(){ bfs(0, 0); //从起点开始搜索 return 0; } ``` 在上述代码中,我们使用广度优先搜索算法从起点开始遍历迷宫,并且使用visited数组记录节点是否被访问过。在搜索过程中,我们向四个方向进行搜索,直到找到终点或者无法继续搜索为止。如果找到了终点,则输出"Find the way! The shortest path length is xxx",其中xxx表示最短路径的长度,搜索结束。如果无法到达终点,则输出"Can't find the way!"。 希望这个例子可以帮助您理C++使用广度优先搜索算法解决迷宫问题
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值