跳到正文

个人记录

7-8课程解析+总结

xiangrui0305约 15 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年4月19日
篇幅
约 15 分钟

版本

  1. v1当前

7-8课程解析+总结

题目传送门

T628045 T628044 P2360 D3443 P1443 P1699

题目解析+总结

T628045

有一个5×55 \times 5迷宫,其中的1表示墙壁,0表示空地,可以通过。可以通过上、下、左、右这四个移动,通过这个迷宫。 现在,从左上角开始,到右上角通过这迷宫,怎样做才能保证路线最短呢?

分析

当我们见到走迷宫求最短路径时,用BFS最好。因为它不会回溯,最先出来的答案一定是最优解,能大大的节省时间。 但又出现求走法时,就需要考虑一下DFS了。求走法、求最短路、求路径总数等,都适用于DFS。 我们选择DFS后,从起点(1,1)开始遍历,在一般情况下,优先级是:左下右上。

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

用struct类型的vector存储这下一步的坐标(当check函数判断成功后),回溯时把它删掉。

C++
vis[xx][yy] = 1;
cnt++;
op.push_back({xx,yy});
DFS(xx,yy);
vis[xx][yy] = 0;
op.pop_back();
cnt--;

若出现答案(即到达终点),则直接输出,然后打断所有程序。输出的程序可以写在函数里面。

C++
cout<<"(0, 0)\n";
for(int i=0;i<cnt;++i){
    cout<<'('<<op[i].x-1<<", "<<op[i].y-1<<')'<<endl;
}
exit(0);

注意:坐标从0开始!!!

代码
C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int a[10][10],cnt = 0;
bool vis[10][10];
struct node{
	int x,y;
};vector<node>op;
int dx[] = {0,1,0,-1};
int dy[] = {-1,0,1,0};
bool check(int x,int y){
	return x >= 1 && x <= 5 && y >= 1 && y <= 5 && !vis[x][y] && a[x][y] != 1;
}
void print(){
	cout<<"(0, 0)\n";
	for(int i=0;i<cnt;++i){
		cout<<'('<<op[i].x-1<<", "<<op[i].y-1<<')'<<endl;
	}
	return ;
}
void DFS(int x,int y){
	if(x == 5 && y == 5){
		print();
		exit(0);
	}
	for(int i=0;i<4;++i){
		int xx = dx[i] + x;
		int yy = dy[i] + y;
		if(check(xx,yy)){
			vis[xx][yy] = 1;
			cnt++;
			op.push_back({xx,yy});
			DFS(xx,yy);
			vis[xx][yy] = 0;
			op.pop_back();
			cnt--;
		}
	}
}
signed main(){
	for(int i=1;i<=5;++i){
		for(int j=1;j<=5;++j){
			cin>>a[i][j];
		}
	}
	vis[1][1] = 1;
	DFS(1,1);
	return 0;
}

T628044

给定一个n×mn \times m的迷宫,同样的,其中标记为 1 的为障碍,标记为 0 的为可以通行的地方。迷宫的入口为左上角,出口为右下角。在迷宫中,只能从一个位置走到这个它的上、下、左、右四个方向之一,D、U、L、R分别表示向下、向上、向左、向右走。

代码
010000
000100
001001
110000

如图所示,从入口开始,可以按 DRRURRDDDR 的顺序通过迷宫。请找出一种通过迷宫的方式,其使用的步数最少。

分析

此时,DFS已经行不通了,有可能找到的不是我们所需要的答案,所以会添加许多麻烦。 这时,还是BFS更胜一筹,因为它最适合找最短路径。可将走法存储起来,该怎么办呢? 我们可以在结构体中加入一个string类型变量s,他可以存储走的方向,符合题意。 后面就简单啦。只要找到出口,直接输出字符串就可以了。

代码
C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
char a[5001][5001];
int cnt = 0,n,m;
bool vis[5001][5001];
int dx[] = {0,1,0,-1};
int dy[] = {-1,0,1,0};
bool check(int x,int y){
	return x >= 1 && x <= n && y >= 1 && y <= m && !vis[x][y] && a[x][y] == '0';
}
struct node{
	int x,y;
	string c;
};queue<node>op;
signed main(){
	cin>>n>>m;
	for(int i=1;i<=n;++i){
		for(int j=1;j<=m;++j){
			cin>>a[i][j];
		}
	}
	vis[1][1] = 1;
	op.push({1,1,""});
	while(op.size()){
		node top = op.front();
		op.pop();
		if(top.x == n && top.y == m){
			cout<<top.c<<endl;
			exit(0);
		}
		for(int i=0;i<4;++i){
			int xx = dx[i] + top.x;
			int yy = dy[i] + top.y;
			if(check(xx,yy)){
				vis[xx][yy] = 1;
				if(i == 0) op.push({xx,yy,top.c+'L'});
				if(i == 1) op.push({xx,yy,top.c+'D'});
				if(i == 2) op.push({xx,yy,top.c+'R'});
				if(i == 3) op.push({xx,yy,top.c+'U'});
			}
		}
	}
	cout<<-1<<endl;
	return 0;
}

P2360

有一个n×m×kn \times m \times k的三维迷宫,其中nn表示迷宫层数,mm表示迷宫行数,kk表示迷宫列数。 当小格没有被岩石所占据时,你可以前往所在小格的前方,后方,左方,右方,上层,下层的小格。每走一小格花费一分钟时间。 如果能顺利逃出迷宫,请输出总用时,否则输出Trapped!

分析

首先定义三个方向数组。

C++
int dx[] = {0,0,0,0,-1,1};
int dy[] = {-1,1,0,0,0,0};
int dz[] = {0,0,-1,1,0,0};

然后,从起点开始遍历,每遍历一次,就记录下用时。

C++
int xx = dx[i] + top.x;
int yy = dy[i] + top.y;
int z2 = dz[i] + top.z;

剩下的就简单多了。若check函数判断成功,则标记下一步的位置;如果找到答案,直接输出总用时,见题面。

代码
C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m,k,sx,sy,sz,zx,zy,zz,minn = 1e9;
char a[31][31][31];
bool vis[31][31][31];
int dx[] = {0,0,0,0,-1,1};
int dy[] = {-1,1,0,0,0,0};
int dz[] = {0,0,-1,1,0,0};
struct node{
	int x,y,z,c;
};
queue<node>op;
bool check(int x,int y,int z){
	return x >= 1 && x <= n && y >= 1 && y <= m && z >= 1 && z <= k && !vis[x][y][z] && a[x][y][z] != '#';
}
signed main(){
	cin>>n>>m>>k;
	for(int i=1;i<=n;++i){
		for(int j=1;j<=m;++j){
			for(int f=1;f<=k;++f){
				cin>>a[i][j][f];
				if(a[i][j][f] == 'S'){
					sx = i;
					sy = j;
					sz = f;
				}
				if(a[i][j][f] == 'E'){
					zx = i;
					zy = j;
					zz = f;
				}
			}
		}
	}
	op.push({sx,sy,sz,0});
	vis[sx][sy][sz] = 1;
	while(op.size()){
		node top = op.front();
		op.pop();
		if(top.x == zx && top.y == zy && top.z == zz){
			cout<<"Escaped in "<<top.c<<" minute(s).";
			return 0;
		}
		for(int i=0;i<6;++i){
			int xx = dx[i] + top.x;
			int yy = dy[i] + top.y;
			int z2 = dz[i] + top.z;
			if(check(xx,yy,z2)){
				vis[xx][yy][z2] = 1;
				op.push({xx,yy,z2,top.c+1});
			}
		}
	}
	cout<<"Trapped!";
	return 0;
}

D3443

代码
C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m,a[155][155],sx,sy,zx,zy;
bool vis[155][155];
int dx[] = {-1,1,0,0};
int dy[] = {0,0,-1,1};
bool check(int x,int y){
	return x >= 1 && x <= n && y >= 1 && y <= m && !vis[x][y] && a[x][y] == 0;
}
struct node{
	int x,y;
	string s;
};queue<node>op;
void print(string s){
	int len = s.size();
	int xx = sx,yy = sy;
    cout<<'('<<sx<<','<<sy<<')';
	for(int i=0;i<len;++i){
		if(s[i] == 'U') xx -= 1;
		if(s[i] == 'D') xx += 1;
		if(s[i] == 'L') yy -= 1;
		if(s[i] == 'R') yy += 1;
		cout<<"->("<<xx<<','<<yy<<')';
	}
	return ;
}
signed main(){
	cin>>n>>m;
	for(int i=1;i<=n;++i){
		for(int j=1;j<=m;++j) cin>>a[i][j];
	}
	cin>>sx>>sy>>zx>>zy;
	vis[sx][sy] = 1;
	op.push({sx,sy,""});
	while(op.size()){
		node top = op.front();
		op.pop();
		if(top.x == zx && top.y == zy){
			print(top.s);
			return 0;
		}
		for(int i=0;i<4;++i){
			int xx = dx[i] + top.x;
			int yy = dy[i] + top.y;
			if(check(xx,yy)){
				vis[xx][yy] = 1;
				if(i == 0) op.push({xx,yy,top.s+'U'});
				if(i == 1) op.push({xx,yy,top.s+'D'});
				if(i == 2) op.push({xx,yy,top.s+'L'});
				if(i == 3) op.push({xx,yy,top.s+'R'});
			}
		}
	}
	cout<<"no way"<<endl;
	return 0;
}

P1443

代码
C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int vis[405][405],n,m,xf,yf;
int dx[] = {1,1,-1,-1,2,2,-2,-2};
int dy[] = {2,-2,2,-2,1,-1,1,-1};
bool check(int x,int y){
	return x >= 1 && x <= n && y >= 1 && y <= m && vis[x][y] == -1;
}
struct node{
	int x,y,c;
};queue<node>op;
signed main(){
	memset(vis,-1,sizeof vis);
	cin>>n>>m>>xf>>yf;
	vis[xf][yf] = 0;
	op.push({xf,yf,0});
	while(op.size()){
		node top = op.front();
		op.pop();
        // cout<<op.size()<<endl;
		for(int i=0;i<8;++i){
			int xx = dx[i] + top.x;
			int yy = dy[i] + top.y;
			if(check(xx,yy)){
				vis[xx][yy] = top.c + 1;
				op.push({xx,yy,top.c+1});
			}
		}
	}
	for(int i=1;i<=n;++i){
		for(int j=1;j<=m;++j){
			cout<<vis[i][j]<<' ';
		}
		cout<<endl;
	}
	return 0;
}

P1699

代码
C++
#include<bits/stdc++.h>
using namespace std;
char a[11][11];
bool vis[11][11];
int lx,ly,bx,by;
struct node{
	int x,y,val;
};
queue<node>op;
bool check(int x,int y){
	return x >= 1 && x <= 10 && y >= 1 && y <= 10 && a[x][y] != 'R' && vis[x][y] == 0;
}
int main(){
	for(int i=1;i<=10;++i){
		for(int j=1;j<=10;++j){
			cin>>a[i][j];
			if(a[i][j] == 'L'){
				lx = i;
				ly = j;
				op.push({lx,ly,0});
				vis[lx][ly] = 1;
			}
			if(a[i][j] == 'B'){
				bx = i;
				by = j;
			}
		}
	}
	while(1){
		node top = op.front();
		op.pop();
		if(top.x == bx && top.y == by){
			cout<<top.val-1<<endl;
			return 0;
		}
		int dx[] = {-1,1,0,0};
		int dy[] = {0,0,-1,1};
		for(int i=0;i<4;++i){
			int xx = dx[i] + top.x;
			int yy = dy[i] + top.y;
			if(check(xx,yy)){
				op.push({xx,yy,top.val+1});
				vis[xx][yy] = 1;
			}
		}
	}
	return 0;
}
原文