第十届蓝桥杯 填空题之走迷宫(BFS DFS) 代码实现

本文介绍了一种使用广度优先搜索(BFS)和深度优先搜索(DFS)解决迷宫寻路问题的方法。通过定义迷宫状态并利用两种不同的搜索策略找到从起点到终点的最短路径。文章提供了完整的C++实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

010000
000100
001001
110000

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 

代码: 

BFS    代码

#include <iostream>
#include <algorithm>
#include <queue>
#define debug(x) cerr<<#x<<"="<<x<<endl
using namespace std;
#define pb push_back
#define int long long
int n, m;
char s[60][60]; bool v[60][60];
struct node {
	int x, y;
	int step;
	string path;
}record[100000];
//D L R U
int dx[] = { 1,0,0,-1 };
int dy[] = { 0,-1,1,0 };

bool ok(int x, int y) {
	if (x >= 1 && x <= n && y >= 1 && y <= m && !v[x][y] && s[x][y] == '0')return true;
	return false;
}
char dir(int x, int y) {
	if (x == 1 && y == 0)return 'D';
	if (x == 0 && y == -1)return 'L';
	if (x == 0 && y == 1)return 'R';
	if (x == -1 && y == 0)return 'U';
}
int mnlen = 1e9, cnt;
queue<node>q;
void bfs() {

	while (q.size()) {
		node temp = q.front(); q.pop();
		if (temp.x == n && temp.y == m) {
			cout << temp.step << endl;
			//cout << temp.path.size() << endl;
			for (int i = 0; i < temp.path.size(); i++)cout << temp.path[i];
			return;
		}

		for (int i = 0; i < 4; i++) {
			int nowx = temp.x + dx[i];
			int nowy = temp.y + dy[i];
			string nowpath = temp.path;
			if (ok(nowx, nowy)) {
				//cout << nowx << " " << nowy << endl;
				v[nowx][nowy] = true;// for book
				node Next;
				Next.x = nowx; Next.y = nowy; Next.step = temp.step + 1; Next.path += nowpath+dir(dx[i], dy[i]);
				q.push(Next);
			}
		}
	}
}
signed main() {
	cin >> n >> m;
	for (int i = 1; i <= n; i++)cin >> s[i] + 1;

	node fir;
	fir.x = 1; fir.y = 1; fir.step = 0; v[1][1] = true;
	q.push(fir);

	//cout << q.size() << endl;
	bfs();



	return 0;
}

DFS 代码 (会爆栈)数据范围大

#include <iostream>
#include <algorithm>
#define debug(x) cerr<<#x<<"="<<x<<endl
using namespace std;
#define pb push_back
#define int long long
int n, m;
char s[60][60]; bool v[60][60];
string path[1000]; int num; string temp; int ct[1000]; int nm;
//D L R U
int dx[] = { 1,0,0,-1 };
int dy[] = { 0,-1,1,0 };

bool ok(int x, int y) {
	if (x >= 1 && x <= n && y >= 1 && y <= m && !v[x][y] && s[x][y] == '0')return true;
	return false;
}
char dir(int x, int y) {
	if (x == 1 && y == 0)return 'D';
	if (x == 0 && y == -1)return 'L';
	if (x == 0 && y == 1)return 'R';
	if (x == -1 && y == 0)return 'U';
}
int mnlen = 1e9, cnt;

void dfs(int x, int y) {
	if (x == n && y == m) {
		ct[nm++] = cnt;
		path[num++] = temp;
		if (mnlen < cnt)mnlen = cnt;
		return;
	}

	for (int i = 0; i < 4; i++) {
		int nowx = x + dx[i];
		int nowy = y + dy[i];
		if (ok(nowx, nowy)) {
			cnt++;
			v[nowx][nowy] = true;
			temp += dir(dx[i], dy[i]);
			dfs(nowx, nowy);
			v[nowx][nowy] = false;
			temp.pop_back();
			cnt--;
		}
	}
}

signed main() {
	cin >> n >> m;
	for (int i = 1; i <= n; i++)cin >> s[i] + 1;

	/*for (int i = 1; i <= n; i++) {  //for check
		for (int j = 1; j <= m; j++) {
			if (ok(i,j))cout << 0;
			else cout << "1";
		}
		cout << endl;
	}*/

	/*cout << endl;
	for (int i = 1; i <= n; i++)  //for check
		cout << s[i] + 1 << endl;*/


	dfs(1, 1);
	v[1][1] = 1;
	for (int i = 0; i < num; i++) {
		cout << ct[i] << " ";
		for (int j = 0; j < path[i].size(); j++)cout << path[i][j];
		cout << endl;
	}

	return 0;
}

谢谢

### 关于第十届蓝桥杯大赛的信息 #### 赛事概述 蓝桥杯是一项面向全国大学生的编程竞赛活动,旨在推动信息技术教育的发展并选拔优秀的程序设计人才。第十届蓝桥杯大赛吸引了众多高校学生参与,并提供了多种语言类别供选手选择,包括Java、C++等。 #### 题目解析与解题思路 ##### Java 类别题目分析 在第十届蓝桥杯的大赛中,Java类别的题目涵盖了多个方面的能力测试。例如,有一道结果填空题要求参赛者计算出一个由特定字母组成的字符串作为答案[^2]。这类题目通常考察的是算法逻辑以及对基本数据结构的理解能力。 对于此类问题解决方法可以采用模拟法或者数学推导等方式来得出结论。具体实现时需要注意边界条件处理以及效率优化等方面的问题。 ##### C++ 类别题目举例说明 ###### 试题 E:迷宫 此题属于典型的图论应用范畴内的最短路径寻找问题。其主要解决方案采用了广度优先搜索(BFS)技术来进行求解过程中的状态转移记录操作[^4]。通过建立二维数组存储地图信息, 并利用队列辅助完成逐层遍历直至目标位置被访问为止; 同时还需要额外开辟空间用来保存每一步移动方向以便最后能够逆向追踪形成完整的行路线序列。 以下是基于上述描述的一个简化版伪代码表示形式: ```cpp #include <iostream> using namespace std; int main(){ int maze[105][105]; // 初始化迷宫矩阵大小为105*105 bool visited[105][105]={false}; // 访问标记初始化为未访问 char route[105*105]; // 存储路径字符 queue<pair<int,int>> q; while(!q.empty()){ pair<int,int> current = q.front(); q.pop(); if(current.first==endX && current.second==endY){ break;// 找到终点退出循环 } // 上下左右四个可能的动作尝试扩展新节点加入队列... } cout<<route<<endl; } ``` ###### 试题 A:平方和 该问题是关于统计满足一定条件下整数集合性质的研究课题之一。给定区间范围内的所有符合条件(即不含指定数字之外其他任何一位组成元素)自然数值列表之后累加它们各自二次幂值得总合得到最终结果值[^5]。 一种可行的做法是从最小值开始逐一判断当前候选对象是否符合筛选标准再决定是否纳入累积变量之中;与此同时也要记得及时更新计数器反映实际有效成员数量变化情况从而便于后续验证正确性与否提供依据支持。 --- ###
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值