跳到正文

个人记录

2025.07.09课堂总结

huyunshen约 9 分钟
原文
文章信息与版本

文章信息

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

版本

  1. v1当前

2025.07.09课堂总结

重点一

以P2360地下城主为例,讲了三维广搜的写法及其注意事项,这类的题目通过率并不高,主要在于:
1.x, y, z坐标存储错误,导致答案出错;
2.方向标记数组出错;
3. …
代码:

C++
#include <bits/stdc++.h>
using namespace std;
const int maxn = 55;
int L, R, C; 
char a[maxn][maxn][maxn];
int sx, sy, sz;
int ex, ey, ez;
/*
start
end
*/
struct Node {
    int x, y, z;
};
/*
a[k][i][j] k层i行j列
*/
bool vis[maxn][maxn][maxn];
// vis[z][x][y] 
bool check(int x, int y, int z) // x是行 y是列 z是层
{
    return (x >= 1 && x <= R && y >= 1 && y <= C && z >= 1 && z <= L &&
            vis[z][x][y] == 0 && a[z][x][y] != '#'); // 本题有多个可以走的符号
}

void bfs()
{
    // 六个方向:前后左右上下
    int dx[] = {1, -1, 0, 0, 0, 0};
    int dy[] = {0, 0, 1, -1, 0, 0};
    int dz[] = {0, 0, 0, 0, 1, -1};
    queue<Node> q;
    memset(vis, 0, sizeof(vis));
    q.push({sx, sy, sz});
    vis[sz][sx][sy] = 1;
    int step = 0;
    while (!q.empty()) {
        int szq = q.size();
        while (szq--) {
            Node cur = q.front(); q.pop();
            if (cur.x == ex && cur.y == ey && cur.z == ez) {
                cout << "Escaped in " << step << " minute(s)." << endl;
                return;
            }
            for (int d = 0; d < 6; ++d) {
                int nx = cur.x + dx[d];
                int ny = cur.y + dy[d];
                int nz = cur.z + dz[d];
                if (check(nx, ny, nz)) {
                    vis[nz][nx][ny] = 1;
                    q.push({nx, ny, nz});
                }
            }
        }
        step++;
    }
    cout << "Trapped!" << endl;
    return;
}

int main()
{
    cin >> L >> R >> C;
    for (int k = 1; k <= L; k++)
    {
        for (int i = 1; i <= R; i++)
        {
            for (int j = 1; j <= C; j++)
            {
                cin >> a[k][i][j];
                if (a[k][i][j] == 'S')
                {
                    sx = i;
                    sy = j;
                    sz = k;
                }
                if (a[k][i][j] == 'E')
                {
                    ex = i;
                    ey = j;
                    ez = k;
                }
            }
        }
    }
    bfs();
    return 0;
}

重点二

以绿题P10487 Nightmare II为例,讲了曼哈顿距离和欧里几得距离,定义如下:
曼哈顿距离:曼哈顿距离是两点在纵轴和横轴上距离的总和。
欧里几得距离:在欧几里得空间中,两点之间的直线距离。

公式如下:

曼哈顿距离:

d(x,y)=∣x1−y1∣+∣x2−y2∣d(x, y) = |x1 - y1| + |x2 - y2|

欧里几得距离:

d=(x2−x1)2+(y2−y1)2d = \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2}

代码:

C++
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1005;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
char a[maxn][maxn];
int dis1[maxn][maxn], dis2[maxn][maxn];
bool vis[maxn][maxn];
int sx1, sy1, sx2, sy2;
struct P{
	int x, y;
};
vector<P> op;
int n, m;
bool run_kill(int x, int y, int t){
	for(int i = 0; i <= 1; i++){
		if(abs(op[i].x - x) + abs(op[i].y - y) <= 2 * t){
			return false;
		}
	}
	return true;
}
bool check(int x, int y){
	return (x >= 1 && x <= n && y >= 1 && y <= m && vis[x][y] == 0 && a[x][y] != 'X');
}
void bfs(){
	memset(vis, 0, sizeof(vis));
	memset(dis1, 0, sizeof(dis1));
	memset(dis2, 0, sizeof(dis2));
	queue<P> Q1;
	Q1.push({sx1, sy1});
	vis[sx1][sy1] = 1;
	dis1[sx1][sy1] = 0;
	int now_t = 0;
	while(!Q1.empty()){
		int len = 3;
		now_t++;
		while(len--){
			int tmp = Q1.size();
			for(int i = 1; i <= tmp; i++){
				P u = Q1.front();
				Q1.pop();
				for(int j = 0; j < 4; j++){
					int xx = u.x + dx[j];
					int yy = u.y + dy[j];
					if(check(xx, yy) && run_kill(u.x, u.y, now_t)){
						dis1[xx][yy] = now_t;
						vis[xx][yy] = 1;
						Q1.push({xx, yy});
					}
				}
			}
		}
	}
	memset(vis, 0, sizeof(vis));
	now_t = 0;
	queue<P> Q2;
	Q2.push({sx2, sy2});
	vis[sx2][sy2] = 1;
	dis2[sx2][sy2] = 0;
	while(!Q2.empty()){
		int len = 1;
		now_t++;
		while(len--){
			int tmp = Q2.size();
			for(int i = 1; i <= tmp; i++){
				P u = Q2.front();
				Q2.pop();
				for(int j = 0; j < 4; j++){
					int xx = u.x + dx[j];
					int yy = u.y + dy[j];
					if(check(xx, yy) && run_kill(u.x, u.y, now_t)){
						dis2[xx][yy] = now_t;
						vis[xx][yy] = 1;
						Q2.push({xx, yy});
					}
				}
			}
		}
	}
	int minn = 2e9;
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= m; j++){
			if(dis1[i][j] != 0 && dis2[i][j] != 0){
				int t = max(dis1[i][j], dis2[i][j]);
				minn = min(t, minn);
			}
		}
	}
	if(minn == 2e9){
		cout << -1 << "\n";
	}else{
		cout << minn <<"\n";
	}
	return ;
}
void solve(){
	cin >> n >> m;
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= m; j++){
			cin >> a[i][j];
			if(a[i][j] == 'M'){
				sx1 = i, sy1 = j;
			}
			if(a[i][j] == 'G'){
				sx2 = i, sy2 = j;
			}
			if(a[i][j] == 'Z'){
				op.push_back({i, j});
			}
		}
	}
	bfs();
	op.clear();
	return ;
}
int main(){
	int T;
	cin >> T;
	while(T--){
		solve();
	}
	return 0;
}

这次的总结就写到这了,感谢您的观看!😊😊😊

原文