蓝桥杯迷宫

在这里插入图片描述
maze.txt

01010101001011001001010110010110100100001000101010
00001000100000101010010000100000001001100110100101
01111011010010001000001101001011100011000000010000
01000000001010100011010000101000001010101011001011
00011111000000101000010010100010100000101100000000
11001000110101000010101100011010011010101011110111
00011011010101001001001010000001000101001110000000
10100000101000100110101010111110011000010000111010
00111000001010100001100010000001000101001100001001
11000110100001110010001001010101010101010001101000
00010000100100000101001010101110100010101010000101
11100100101001001000010000010101010100100100010100
00000010000000101011001111010001100000101010100011
10101010011100001000011000010110011110110100001000
10101010100001101010100101000010100000111011101001
10000000101100010000101100101101001011100000000100
10101001000000010100100001000100000100011110101001
00101001010101101001010100011010101101110000110101
11001010000100001100000010100101000001000111000010
00001000110000110101101000000100101001001000011101
10100101000101000000001110110010110101101010100001
00101000010000110101010000100010101001000100010101
10100001000110010001000010101001110101011111010010
00000100101000000110010100101001100001000000000010
11010000001001110111001001000011101001011011101000
00000110100010001000100000001000011101000000110011
10101000101000100010001111100010101001010000001000
10000010100101001010110000000100101010001011101000
00111100001000010000000110111000000001000000001011
10000001100111010111010001000110111010101101111000

【题目解析】
(1)最短路径可能不止一条,不过并不复杂。BFS的特点是:它是逐层扩散的(往BFS的队列中加入邻居结点时,是按距离起点远近的顺序加入的:先加入距离起点为1的邻居结点,加完之后,再加入距离为2的邻居结点,等等),搜完一层,才会继续搜下一层。一条路径是从起点开始,沿着每一层逐步往外走,每多一层,路径长度就增加了1。那么,所有长度相同的最短路径都是从相同的层次扩散出去的。当搜到第一个到达终点的最短路径后,继续搜索,会返回其他可能不是最短的路径。

(2)题目要求返回字典序最小的最短路径,那么只要在每次扩散下一层(往BFS的队列中加入下一层的结点)时,都按字典序“D<L<R<U”的顺序来加下一层的结点,那么第一个搜到的最短路径就是字典序最小的那个。

两种路径打印方法
一、简单方法
路径如何打印?一个简单的办法是:每扩展到一个点v,都在v上存储从起点s到v的完整路径path。到达终点t时,就得到了从起点s到t的完整路径。这样做的缺点是会占用大量空间,因为每个点上都存储了完整的路径。

#include<iostream>
#include<queue>
#include<cstdio>
#include<string>
#include<cstring>

using namespace std;

char map[35][55];//地图
int vis[35][55];//遍历标记

int dir[4][2] = {//根据字典序访问下一个
	{1,0},//D
	{0,-1},//L
	{0,1},//R
	{-1,0},//U
};

char dic[4] = { 'D','L','R','U' };

int x1, x2, y11, y2, m, n;//(x1,y11)起点,(x2,y2)终点 

struct node {
	int x, y;
	int step;//到达该节点的步数
	string path;//到达该点路径
};

void bfs() {
	queue<node> q;
	node p;//当前所在节点
	p.x = x1; p.y = y11; p.step = 0; p.path = "";
	vis[x1][y11] = 1;
	q.push(p);
	while (!q.empty()) {
		node p = q.front();//队首节点
		if (p.x == x2 && p.y == y2) {
			if (p.step == 0) {

			}
			cout << p.step << endl << p.path;
		}
		node v;//下一步要去的节点
		for (int i = 0; i < 4; i++) {
			v.x = p.x + dir[i][0];
			v.y = p.y + dir[i][1];
			if (v.x >= 0 && v.y >= 0 && v.x < n && v.y < m && vis[v.x][v.y] == 0 && map[v.x][v.y] != '1') {
				vis[v.x][v.y] = 1;
				v.step = p.step + 1;
				v.path = p.path + dic[i];
				q.push(v);
			}
		}
		q.pop();
	}
}

int main() {
	n = 30, m = 50;
	memset(map, '1' , sizeof(map));
	memset(vis, 0, sizeof(vis));
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> map[i][j];
		}
	}
	x1 = 0;
	y11 = 0;
	x2 = n-1; y2 = m-1;
	bfs();
	return 0;

}

二、标准方法
注意看 print_path,它是递归函数,先递归再打印。从终点开始,回溯到起点后,再按从起点到终点的顺序,正序打印出完整路径。

#include<iostream>
#include<queue>
#include<cstdio>
#include<string>
#include<cstring>

using namespace std;

char map[35][55];//地图
int vis[35][55];//遍历标记
char pre[35][55];//用于查找前驱节点

int dir[4][2] = {//根据字典序访问下一个
	{1,0},//D
	{0,-1},//L
	{0,1},//R
	{-1,0},//U
};

char dic[4] = { 'D','L','R','U' };

int x1, x2, y11, y2, m, n;//(x1,y11)起点,(x2,y2)终点 

struct node {
	int x, y;
	int step;//到达该节点的步数
};

void print_path(int x, int y) {
	if (x == 0 && y == 0)return;
	if (pre[x][y] == 'D')print_path(x - 1, y);//向上回溯
	if (pre[x][y] == 'L')print_path(x, y+1);//向右回溯
	if (pre[x][y] == 'R')print_path(x, y-1);//向左回溯
	if (pre[x][y] == 'U')print_path(x + 1, y);//向下回溯
	printf("%c", pre[x][y]);

}

void bfs() {
	queue<node> q;
	node p;//当前所在节点
	p.x = x1; p.y = y11; p.step = 0; 
	vis[x1][y11] = 1;
	q.push(p);
	while (!q.empty()) {
		node p = q.front();//队首节点
		if (p.x == x2 && p.y == y2) {
			if (p.step == 0) {

			}
			cout << p.step << endl;
			print_path(29, 49);
			return;
		}
		node v;//下一步要去的节点
		for (int i = 0; i < 4; i++) {
			v.x = p.x + dir[i][0];
			v.y = p.y + dir[i][1];
			if (v.x >= 0 && v.y >= 0 && v.x < n && v.y < m && vis[v.x][v.y] == 0 && map[v.x][v.y] != '1') {
				vis[v.x][v.y] = 1;
				v.step = p.step + 1;
				pre[v.x][v.y] = dic[i];
				q.push(v);
			}
		}
		q.pop();
	}
}

int main() {
	n = 30, m = 50;
	memset(map, '1' , sizeof(map));
	memset(vis, 0, sizeof(vis));
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> map[i][j];
		}
	}
	x1 = 0;
	y11 = 0;
	x2 = n-1; y2 = m-1;
	bfs();
	return 0;

}

186
DDDDRRURRRRRRDRRRRDDDLDDRDDDDDDDDDDDDRDDRRRURRUURRDDDDRDRRRRRRDRRURRDDDRRRRUURUUUUUUULULLUUUURRRRUULLLUUUULLUUULUURRURRURURRRDDRRRRRDDRRDDLLLDDRRDDRDDLDDDLLDDLLLDLDDDLDDRRRRRRRRRDDDDDDRR

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值