跳到正文

个人记录

7.8_401_课堂总结

Railgun_约 11 分钟
原文
文章信息与版本

文章信息

发布
2025年7月8日
收录
2025年12月12日
更新
2026年11月8日
篇幅
约 11 分钟

版本

  1. v1当前

T628045 迷宫问题

题目传送门

题目描述

定义一个二维数组:

C++
int maze[][] = {
    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表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。

输入格式

一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。

输出格式

左上角到右下角的最短路径,格式如下。

C++
(x1, y1)
(x2, y2)
(x3, y3)
  ....
(xN, yN)

题目分析

这道题就是广搜+保存路径

广搜就是一个板子广搜,而保存路径可以用一个结构体:

C++
struct Node{
	int x, y;
	string s;
};

其中,s就是用来保存路经的

然后,在BFS里面初始化队列时,将路径初始化为"(0, 0)",每走一步就加上"(x - 1, y - 1)",在C++里写为:"(" + to_string(x - 1) + ", " + to_string(y - 1) + ")"

注意:to_string一定要在C++11及以上使用,否则会CE\textbf{\color{red}注意:to\_string一定要在C++11及以上使用,否则会CE}

在输出答案时,直接输出保存的路径即可:

C++
Node f = que.front();//用变量来保存当前搜索的坐标及路径
que.pop();
if(f.x == n && f.y == m){//如果走到终点
    cout << f.s;//输出保存的路径
    return;//结束BFS
}

然后这道题就做完了,以下是参考代码:

C++
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int n, m, fx, fy;
int a[101][101];
bool vis[101][101];
int dx[] = {0, -1, 0, 1};
int dy[] = {-1, 0, 1, 0};

struct Node{
	int x, y;
	string s;
};
 
void bfs(int x, int y){
	queue<Node> que;
	que.push({x, y, "(0, 0)"});
	vis[x][y] = true;
	while(que.size()){
		Node f = que.front();
		que.pop();
		if(f.x == n && f.y == m){
			cout << f.s;
			return;
		}
		for(int i = 0; i < 4; i++){
			int tx = f.x + dx[i];
			int ty = f.y + dy[i];
			if(tx > 0 && ty > 0 && tx <= n && ty <= m && vis[tx][ty] == false && a[tx][ty] == 0){
				que.push({tx, ty, f.s + "\n(" + to_string(tx - 1) + ", " + to_string(ty - 1) + ")"});
				vis[tx][ty] = true; 
			}
		}
	} 
}

int main(){
	n = 5, m = 5;
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= m; j++){
			cin >> a[i][j];
		}
	}
	bfs(1, 1); 
	return 0;
} 

T628044 迷宫之输出路径-困难版

题目传送门

题目描述

下图给出了一个迷宫的平面图,其中标记为 11 的为障碍,标记为 00 的为可以通行的地方。

代码
010000
000100
001001
110000

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

对于下面这个更复杂的迷宫( nn 行 mm 列),请找出一种通过迷宫的方式,其使用的步数最少,在步数最少的前提下,请找出字典序最小的一个作为答案。

请注意在字典序中 D<L<R<UD<L<R<U。

输入格式

第一行输入两个整数 nn 和 mm 表示迷宫的行列数。 接下来 nn 行,每行输入 mm 个字符(00 或 11 表示能够通过),注意中间没有空格隔开。

输出格式

输出一行字符串表示最短路径,且保证字典序最小。 走不到右下角请输出 −1-1。

数据范围

对于1−10个测试点:2≤n,m≤500对于1-10个测试点:2\le n,m\le 500
对于11−12个测试点:2≤n,m≤5000对于11-12个测试点:2\le n,m\le 5000

题目分析

本题与上题比较接近,不同的地方在保存路经方面

这道题保存路径也不是很难,因为题目提到了“路径最近的情况下字典需要最小”,并且题目也提示“在字典序中 D<L<R<UD<L<R<U。”

所以,按照方向的字典序从小到大的顺序搜:先下,然后左,再右,最后上

于是,我们在搜索部分的代码便变成了这样:

C++
if(i == 0){
    que.push({tx, ty, f.s + "D"});//往下搜
}else if(i == 1){
    que.push({tx, ty, f.s + "L"});//往左搜
}else if(i == 2){
    que.push({tx, ty, f.s + "R"});//往右搜
}else{
    que.push({tx, ty, f.s + "U"});//往上搜
}

以下是这题的参考代码:

C++
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int n, m, fx, fy;
char a[5001][5001];
bool vis[5001][5001];
int dx[] = {1, 0, 0, -1};
int dy[] = {0, -1, 1, 0};

struct Node{
	int x, y;
	string s;
};
 
void bfs(int x, int y){
	queue<Node> que;
	que.push({x, y, ""});
	vis[x][y] = true;
	while(que.size()){
		Node f = que.front();
		que.pop();
		if(f.x == n && f.y == m){
			cout << f.s;
			return;
		}
		for(int i = 0; i < 4; i++){
			int tx = f.x + dx[i];
			int ty = f.y + dy[i];
			if(tx > 0 && ty > 0 && tx <= n && ty <= m && vis[tx][ty] == false && a[tx][ty] == '0'){
				if(i == 0){
					que.push({tx, ty, f.s + "D"});
				}else if(i == 1){
					que.push({tx, ty, f.s + "L"});
				}else if(i == 2){
					que.push({tx, ty, f.s + "R"});
				}else{
					que.push({tx, ty, f.s + "U"});
				}
				vis[tx][ty] = true; 
			}
		}
	} 
	cout << -1;
}

int main(){
	cin >> n >> m;
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= m; j++){
			cin >> a[i][j];
		}
	}
	bfs(1, 1); 
	return 0;
} 

P2360 地下城主

题目传送门

题目描述

你参加了一项秘密任务,在任务过程中你被困在了一个 3D 的地下监狱里面,任务是计时的,你现在需要在最短的时间里面从地牢里面逃出来继续你的任务。地牢由若干层组成,每一层的形状都是长宽一样的矩形,地牢被分成了若干小格,当小格没有被岩石所占据时,你可以前往所在小格的前方,后方,左方,右方,上层,下层的小格。每走一小格花费一分钟时间。

你能不能顺利的从地牢里面逃出来呢?如果可以,那所需要的最短时间又是多少呢?

输入格式

第一行输入 L,R,CL,R,C (LL 为地牢的层数,RR 为每层小格的行数,CC 为每层小格的列数,其中 1≤L,R,C≤301 \leq L,R,C \leq 30)。

第二行开始输入 LL 层地牢的格局,每一层有 RR 行,每行有 CC 个小格,两层地牢中间有空行隔开。

输入的文件中,S 代表你的起始点,E 代表你所要到达的出口,# 为小格被岩石占据,. 表示没有被岩石占据。

输出格式

输出包含一行,当你可以顺利到达出口时请输出:Escaped in x minute(s). 其中,xx 代表你所花费的最短时间,否则请输出 Trapped!

题目分析

第一眼:哇!竟然是三维的BFS!

但细看便会发现,与普通的二维BFS有异曲同工之妙,没什么难点

以下是参考代码:

C++
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int n, m, sx, sy, sz, h;
char a[50][50][50];
bool vis[50][50][50];
int dx[] = {1, 0, 0, -1, 0, 0};
int dy[] = {0, -1, 1, 0, 0, 0};
int dz[] = {0, 0, 0, 0, 1, -1};

struct Node{
	int x, y, z, step;
};
 
void bfs(int x, int y, int z){
	queue<Node> que;
	que.push({x, y, z, 0});
	vis[z][x][y] = true;
	while(que.size()){
		Node f = que.front();
		que.pop();
		if(a[f.z][f.x][f.y] == 'E'){
			cout << "Escaped in " << f.step << " minute(s).";
			return;
		}
		for(int i = 0; i < 6; i++){
			int tx = f.x + dx[i];
			int ty = f.y + dy[i];
			int tz = f.z + dz[i];
			if(tx > 0 && ty > 0 && tx <= n && ty <= m && tz > 0 && tz <= h && vis[tz][tx][ty] == false && a[tz][tx][ty] != '#'){
				que.push({tx, ty, tz, f.step + 1});
				vis[tz][tx][ty] = true; 
			}
		}
	} 
	cout << "Trapped!";
}

int main(){
	cin >> h >> n >> m;
	for(int k = 1; k <= h; k++){
		for(int i = 1; i <= n; i++){
			for(int j = 1; j <= m; j++){
				cin >> a[k][i][j];
				if(a[k][i][j] == 'S'){
					sx = i;
					sy = j;
					sz = k;
				}
			}
		}
	}
	bfs(sx, sy, sz); 
	return 0;
} 

THE END

小彩蛋

原文