bfs迷宫问题--打印路径

/*定义一个二维数组: 
int maze[5][5] = {
	0, 1, 0, 0, 0,
	0, 1, 0, 1, 0,
	0, 0, 0, 0, 0,
	0, 1, 1, 1, 0,
	0, 0, 0, 1, 0,
};
它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,
要求编程序找出从左上角到右下角的最短路线
Input
一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。
Output
左上角到右下角的最短路径,格式如样例所示。
Sample Input
0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0
Sample Output
(0, 0)
(1, 0)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 4)
(4, 4)
*/
#include <cstdio>
#include <cstring>
#include <cstdlib>
#define N 5

using namespace std;

struct note{
	int x;
	int y;
	int f;//父节点
	int s;//步数
};

int map[N][N] = { 0 }, vis[N][N] = { 0 };

int dx[4] = { -1,0,0,1 };
int dy[4] = { 0,-1,1,0 };

struct note que[N*N];
int head = 0, tail = 1;//队列初始化

void print(int i)//回溯法输出
{
	if (que[i].f != -1)//临界条件父节点为-1
		print(que[i].f);
	printf("(%d, %d)\n", que[i].x, que[i].y);
	return;
}

void bfs(int startx, int starty)
{
	//往队列插入初始位置坐标(插入时从尾部开始)
	que[head].x = startx;
	que[head].y = starty;
	que[head].f = -1;
	que[head].s = 0;
	vis[startx][starty] = 1;

	while (head < tail)//当队列不空
	{
		for (int i = 0; i < 4; i++)
		{
			int tx = que[head].x + dx[i];
			int ty = que[head].y + dy[i];//下一个点的坐标

			if (tx >= 0 && tx <= 4 && ty >= 0 && ty <= 4 && !vis[tx][ty] && !map[tx][ty])//下一点可以到达
			{
				vis[tx][ty] = 1;//插入到队列中
				que[tail].x = tx;
				que[tail].y = ty;
				que[tail].f = head;//记录父节点
				que[tail].s = que[head].s + 1;////步数是父节点的步数+1
				tail++;//队列的长度进行扩展
			}

			if (tx == 4 && ty == 4)
			{
				print(head);//此时的head为终点前一步
				return;
			}
		}

		head++;//队列首位置出队
	}
}

int main()
{
	for (int i = 0; i < N; i++)//输入地图
	{
		for (int j = 0; j < N; j++)
		{
			scanf("%d", &map[i][j]);
		}
	}
	bfs(0, 0);//广搜
	printf("(4, 4)\n");
	printf("%d\n", que[tail - 1].s);//tail-1对应为终点处。
	return 0;
}

以上为c语言用队列存储路径,下边为前驱数组存储路径。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
#define N 5

int map[N][N];//定义一个map,存储输入的地图 

struct point{
	int x, y;//定义一个点的结构体 
}pre[N][N];//pre后面用于存储父节点 

queue<point> q;
int next[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};

void bfs(int x, int y)
{
	while(!q.empty()) q.pop();//清空队列 
	point start;
	start.x = x, start.y = y;
	q.push(start);//加入队列的首位 
	pre[x][y].x = x; pre[x][y].y = y;//把(0,0)的父节点初始化为(-2,-2) 
		while(!(q.front().x==4 && q.front().y==4))//条件为搜查到终点 
		{
			point from = q.front(); q.pop();//把队列首位赋值给from再删除 
			for(int i=0; i<4; i++)
			{
				point to;//to为下一步 
				to.x = from.x + next[i][0];
				to.y = from.y + next[i][1];//走下一步赋值 
				if(to.x<0 || to.x>=5 || to.y<0 || to.y>=5) continue;//如果越界就不走下一步,不执行 
				if(map[to.x][to.y] || pre[to.x][to.y].x != -1) continue;//如果下一步为障碍或者下一步之前走过,不执行 
				q.push(to);//把to加入队列 
				pre[to.x][to.y].x = from.x; pre[to.x][to.y].y = from.y;//把to节点的父节点from的坐标赋值给pre 
			} 
		}
}

void print(int x, int y)//父节点输出的套路 
{
	if(pre[x][y].x != x || pre[x][y].y != y) //当一个节点的父节点不是他本身时 
		print(pre[x][y].x, pre[x][y].y);
	printf("(%d, %d)\n",x,y);
}
 
int main()
{
	int i, j;
	for(i=0; i<5; i++)
	{
		for(j=0; j<5; j++)
			scanf("%d",&map[i][j]);
	}
	memset(pre,-1,sizeof(pre));//pre为父节点,初始化全部为-1 
	bfs(0,0);//广搜,找出最短路径 
	print(4,4);//输出最短路径 
	return 0;
}

搜索迷宫的存储路径问题:

bfs存路径,最后回溯输出。

方法1:在队列的结构体中加一个f元素,初值赋为-1,以后每次遇到新节点,都将其f元素赋值为之前的点,最后回溯输出的判断条件是

当前元素的f元素不为-1,不断递归调用print函数。

方法2:开一个pre数组,大小与地图一样,起点元素pre【x】【y】=(x,y);以后每次遇到新节点,pre【to.x】【to.y】=(from.x,from.y)

最后回溯输出,递归函数第一次传入的元素为终点坐标,递归终止条件为pre[x][y]=(x,y);不过对终点元素应该单独输出。

总结:利用队列,每次都记录当前节点的父节点,最后回溯输出

dfs存路径。

方法1:如马踏棋盘问题,已知总步数:开一个pre【】数组记录路径的每个节点坐标,每次dfs用pre【step】记录当前元素坐标,

如果找到终点就直接跳出dfs,否则,回溯再找其他dfs的结果(每次调用对pre的路径都会有更新,所以不必担心pre路径重复存储的问题)

方法2:迷宫用dfs存路径:同样的方法用pre【step】记录每一步的元素坐标,遇到终点就跳出dfs,否则回溯找其他dfs路径。

dfs的两种方法可归为同一种,核心就是pre【step】记录每一步的路径,利用dfs的递归性,可以每次对pre数组更新,直到找到合适路径。

    原文作者:迷宫问题
    原文地址: https://blog.csdn.net/yuanba_xs/article/details/54799142
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞