2019年第十届蓝桥杯省赛A组(C/C++组)迷宫(BFS)

试题 D: 迷宫
【问题描述】
下图给出了一个迷宫的平面图,其中标记为 1 的为障碍,标记为 0 的为可 以通行的地方。

010000
000100
001001
110000

迷宫的入口为左上角,出口为右下角,在迷宫中,只能从一个位置走到这 个它的上、下、左、右四个方向之一。
对于上面的迷宫,从入口开始,可以按DRRURRDDDR 的顺序通过迷宫, 一共 10 步。其中 D、U、L、R 分别表示向下、向上、向左、向右走。

对于下面这个更复杂的迷宫(30 行 50 列),请找出一种通过迷宫的方式, 其使用的步数最少,在步数最少的前提下,请找出字典序最小的一个作为答案。 请注意在字典序中D<L<R<U。(如果你把以下文字复制到文本文件中,请务 必检查复制的内容是否与文档中的一致。在试题目录下有一个文件 maze.txt, 内容与下面的文本相同)

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

答案:

DDDDRRURRRRRRDRRRRDDDLDDRDDDDDDDDDDDDRDDRRRURRUURRDDDDRDRRRRRRDRRURRDDDRRRRUURUUUUUUULULLUUUURRRRUULLLUUUULLUUULUURRURRURURRRDDRRRRRDDRRDDLLLDDRRDDRDDLDDDLLDDLLLDLDDDLDDRRRRRRRRRDDDDDDRR

思路:
(1)首先01地图中找最短路,肯定选择广度优先级搜索(BFS)。

(2)然后需要路径,只需要在搜索过程中记录下每个节点由什么操作转换(或者记录前一个节点的二维坐标),之后再反向沿着路径找回去就知道了走法。

(3)最后还需要字典序最小,那么我们可以考虑方向数组的4个方向向量的顺序。想到的方法是,从右下角节点 T(n,m) 开始,向左上角S(1,1) 搜索找路径。同时维护好每个坐标点是如何由上一坐标点到达。

(4)由于字典序最小,那我们安排优先走字典序小的方向,每次都走最小的,那最后结果也是最小的。

(5)由于bfs是记录上一步的坐标位置,所以我们从右下角向左上角来走,每次走最大的,那从左上角到右上角便是最小的啦。

按这样便可以输出路径了!!

这道题确实坑...

(最标准的答案代码在最后)

打印移动步骤
#include<iostream>
#include<cstdio>
#include<string.h>
#include<queue>
using namespace std;
struct box
{
	int x, y;
};
class queue//可以不用定义类,我练习一下
{
public:
	queue() { T = new box[400]; front = 0; rear = 0; }
	void push(box x)
	{
		T[rear] = x;
		rear++;
		
	}
	void pop()
	{
		front++;

		
	}
	bool isempty()
	{
		if (front  == rear)
			return true;
		else false;
	}
	box getfront()
	{
		return T[front];
	}
	box *T;
	int front;
	int rear;
};
int main()
{
	queue qu;//如果不定义的话,换成queue<box> qu;
	box q;
	int path[20][20];
	char maze[20][20];
	bool visit[20][20];
	int output[400][2];//用于输出路径
	memset(visit, false, sizeof(visit));
	int m, n;
	int x0, y0;//记录起点的位置
	int di[4][2] = { {-1,0},{1,0},{0,-1},{0,1} };//上下左右四个方向

	char S[4] = { 'U','D','L','R' };
	bool flag;
	cout << "输入迷宫的行数和列数" << endl;
	
	cin >> m >> n;
	for (int i = 0; i < m; i++)
	{
		for (int j = 0; j < n; j++)
		{
			cin >> maze[i][j];
			if (maze[i][j] == 'E')
			{
				x0 = i, y0 = j;

			}

		}
	}
	//起点进队列
	q.x = x0; q.y = y0;
	qu.push(q);
	visit[q.x][q.y] = true;//已经访问过了
	//开始搜索
	flag = false;
	while (qu.isempty() != true && flag ==false)
	{
		box temp = qu.getfront();
		qu.pop();
		for (int i = 0; i < 4; i++)
		{
			q.x = temp.x + di[i][0];
			q.y = temp.y + di[i][1];
			if (q.x < 0 || q.x >= m || q.y < 0 || q.y >= n)
			{
				continue;
			}
			if (maze[q.x][q.y] != '*'&&visit[q.x][q.y] ==false)//该点能走

			{
				qu.push(q);
				path[q.x][q.y] = i;
				if (maze[q.x][q.y] == 'S')//找到目标
				{
					flag = true;
					break;
				}
				visit[q.x][q.y] = true;
			}
		}
		

	}//搜索结束
	if (flag == true)
	{
		int step = 0;
		while (q.x != x0 || q.y != y0)
		{
			switch (path[q.x][q.y])
			{
			case 0:
				cout << "D";
				q.x = q.x + 1;
				break;
			case 1:
				cout << "U";
				q.x = q.x - 1;
				break;

			case 2:
				cout << "R";
				q.y = q.y + 1;
				break;

			case 3:
				cout << "L";
				q.y = q.y - 1;
				break;
			}
			/*output[step][0] = q.x;
			output[step][1] = q.y;
			int x = q.x;
			q.x = path[q.x][q.y].x;
			q.y = path[x][q.y].y;
			step++;*/
		}
		/*cout << "输出路径为:" << endl;
		for (int i = step - 1; i >= 0; --i)
		{
			cout << "(" << output[i][0] << "," << output[i][1] << ")" << endl;
		}*/
	}
	else
		cout << "迷宫无解" << endl;

	system("pause");
	return 0;
}

相似的迷宫题目

就是上面这道题的基础版本,有利于理解上面一题

Sample Input

15 16

.**.***.***.***

***.***..**.*.*

.**.***.***.***

***.***..**.*.*

**.***.***.***.

***.***.......E

.**.***.***.*.*

***..**...*...*

.*....*.*...*.*

***.*.*.**.*..*

....*...**...**

*.*.***..**.*.*

.S*.***.***.***

***.***..**.*.*

.**.***.***.**.

***..**...*...*

Sample Output

The path is:

(11,1) (10,1) (10,2) (10,3) (9,3) (8,3) (8,4) (8,5) (9,5) (10,5) (10,6) (10,7) (9,7) (8,7) (7,7) (6,7) (5,7) (5,8) (5,9) (5,10) (5,11) (5,12) (5,13) (5,14)

首先输入两个数代表迷宫的行数和列数,‘.’代表可以走的点,'*'代表墙,即不可以走的点。‘S’代表迷宫的入口,‘E’代表迷宫的出口。

由于广搜中,队列中的每一点被搜索到时都是在离起点最短的路径中被搜索到的,所以可以再建立一个以点的坐标为数据元素的二维数组,该二维数组每个数据元素对应迷宫中的每个点,其数据元素的值为该点在离起点最短的路径中的前一个点的坐标。这样迷宫的路径便不再由队列元素保存,所以出队并不影响路径的输出。

思路类似:本质都是利用bfs记录上一步的元素,然后输出

#include<iostream>
#include<cstdio>
#include<string.h>
using namespace std;
struct box
{
	int x, y;
};
class queue
{
public:
	queue() { T = new box[400]; front = 0; rear = 0; }
	void push(box x)
	{
		T[rear] = x;
		rear++;
		
	}
	void pop()
	{
		front++;

		
	}
	bool isempty()
	{
		if (front  == rear)
			return true;
		else false;
	}
	box getfront()
	{
		return T[front];
	}
	box *T;
	int front;
	int rear;
};
int main()
{
	queue qu;
	box q;
	box path[20][20];
	char maze[20][20];
	bool visit[20][20];
	int output[400][2];//用于输出路径
	memset(visit, false, sizeof(visit));
	int m, n;
	int x0, y0;//记录起点的位置
	int di[4][2] = { {-1,0},{1,0},{0,-1},{0,1} };//上下左右四个方向
	bool flag;
	cout << "输入迷宫的行数和列数" << endl;
	
	cin >> m >> n;
	for (int i = 0; i < m; i++)
	{
		for (int j = 0; j < n; j++)
		{
			cin >> maze[i][j];
			if (maze[i][j] == 'S')
			{
				x0 = i, y0 = j;

			}

		}
	}
	//起点进队列
	q.x = x0; q.y = y0;
	qu.push(q);
	visit[q.x][q.y] = true;//已经访问过了
	//开始搜索
	flag = false;
	while (qu.isempty() != true && flag ==false)
	{
		for (int i = 0; i < 4; i++)
		{
			q.x = qu.getfront().x + di[i][0];
			q.y = qu.getfront().y + di[i][1];
			if (q.x < 0 || q.x >= m || q.y < 0 || q.y >= n)
			{
				continue;
			}
			if (maze[q.x][q.y] != '*'&&visit[q.x][q.y] ==false)//该点能走

			{
				qu.push(q);
				path[q.x][q.y] = qu.getfront();
				if (maze[q.x][q.y] == 'E')//找到目标
				{
					flag = true;
					break;
				}
				visit[q.x][q.y] = true;
			}
		}
		qu.pop();

	}//搜索结束
	if (flag == true)
	{
		int step = 0;
		while (q.x != x0 || q.y != y0)
		{
			output[step][0] = q.x;
			output[step][1] = q.y;
			int x = q.x;
			q.x = path[q.x][q.y].x;
			q.y = path[x][q.y].y;
			step++;
		}
		cout << "输出路径为:" << endl;
		for (int i = step - 1; i >= 0; --i)
		{
			cout << "(" << output[i][0] << "," << output[i][1] << ")" << endl;
		}
	}
	else
		cout << "迷宫无解" << endl;

	system("pause");
	return 0;
}

简易版本 

#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<string.h>
using namespace std;
struct box
{
	int x, y;
	box(int a, int b)
	{
		x = a;
		y = b;


	}
	box() {};
};
queue<box> qu;
box q;
box path[20][20];
char maze[20][20];
bool visit[20][20] = {0};
int output[400][2];
int dir[4][2] = { {-1,0},{1,0},{0,-1},{0,1} };

int main()
{
	int x0, y0;
	int m, n;
	cin >> m >> n;
	for (int i = 0; i < m; i++)
	{
		for (int j = 0; j < n; j++)
		{
			cin >> maze[i][j];
			if (maze[i][j] == 'S')
			{
				x0 = i;
				y0 = j;
			}
		}
	}

	bool flag = false;

	queue<box> qu;
	box q(x0, y0);
	qu.push(q);
	visit[x0][y0] = 1;
	int dx, dy;
	while (!qu.empty()&&flag==false)
	{
		box t = qu.front();
		qu.pop();
		
		
		for (int i = 0; i < 4; i++)
		{
			dx = t.x + dir[i][0];
			dy = t.y + dir[i][1];
			if (dx < m&&dy < n&&dx >= 0 && dy >= 0)
			{
				if (maze[dx][dy] != '*'&&visit[dx][dy] == 0)
				{
					box qt(dx, dy);
					path[dx][dy] = t;
					

					qu.push(qt);
					visit[dx][dy] = 1;
					if (maze[dx][dy] == 'E')
					{
						flag = true;
						break;
					}
					
				}
			}
		}
	}

	box t;
	int step = 0;
	int x1;
	
	while (dx!= x0 || dy != y0)
	{
		output[step][0] = dx;
		output[step][1] = dy;
		x1 = dx;
		dx=path[dx][dy].x;
		dy = path[x1][dy].y;

		step++;
		


	}
	for (int i = step - 1; i >= 0; i--)
	{
		cout << "(" << output[i][0] << "," << output[i][1] << ")" << endl;
	}
	system("pause");
	return 0;
}

补充:!!!

确实可以记录上一步的步骤,但我们可以走到某个点,就把这个点的路径设为之前的路径加上走到这一步的路径,最后在直接输出最后一个点的记录的所有路径。

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<set>
using namespace std;
 
#define N 30
#define M 50
 
char map[N][M];
int dir[4][2]={{1,0},{0,-1},{0,1},{-1,0}};//D<L<R<U
char ch[4]={'D','L','R','U'};
int vis[N][M]={0};
 
struct point
{
	int x,y;
	string road;//记录路径到这一个点的路径
	point(int a,int b)
	{
		x=a;
		y=b;
	}
};
 
void bfs()
{
	queue<point> q;
	point p(0,0);
	p.road="";
	q.push(p);
	vis[0][0]=1;
	while(!q.empty())
	{
		point t=q.front();
		q.pop();
		if(t.x==N-1&&t.y==M-1)
		{
			cout<<t.road<<endl;//输出
			break;
		}
		for(int i=0;i<4;i++)
		{
			int dx=t.x+dir[i][0];
			int dy=t.y+dir[i][1];
			if(dx>=0&&dx<N&&dy>=0&&dy<M)
			{
				if(map[dx][dy]=='0'&&!vis[dx][dy])
				{
					point tt(dx,dy);
					tt.road=t.road+ch[i];//记录路径
					q.push(tt);
					vis[dx][dy]=1;
				}
			}
		}
	}
}
 
int main()
{
	for(int i=0;i<N;i++)
	{
		cin>>map[i];
	}
	bfs();
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值