跳到正文

个人记录

5.8下午bfs进阶--东塘--蒋--总结

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

文章信息

发布
2026年5月10日
收录
2026年5月12日
更新
2026年11月11日
篇幅
约 11 分钟

版本

  1. v2当前
  2. v1

考试情况

ABCDE总分
01008200128

错题一:T759360 流感传染

题意

有一个 n * n 的宿舍区,每个格子要么:

  • . 健康的人
  • # 空房间
  • @ 第一天得病的人

每天,得病的人会传染给上下左右相邻的健康人。 空的房间不会传染,也不会被传染。 已经得病的人不会重复统计,并且一直保持得病状态。

问:到了第 m 天之前(也就是第 m-1 天结束时),一共有多少人得过流感?

注意:第 1 天开始的时候只有 @ 的人得病。

思路

  • 把刚开始所有 @ 的位置作为 BFS 起点,标记访问(得病)。
  • 每扩展一层,就说明过了一天。
  • step 表示从初始到当前节点传播所用的天数,比如起点 step=0,它的邻居 step=1,表示在第一天结束后的新感染者。
  • BFS 按层前进,直到 step == m 时停止继续传播,返回结果。

注意:这个 BFS 跟我们通常求最短路的 BFS 不一样,这里 step 就是传播轮次。


代码

C++
#include <bits/stdc++.h>
using namespace std;
const int N = 105;
int n, m;
int ans;
char a[N][N];
bool vis[N][N];
int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};
struct node
{
	int x, y, step;
};
void bfs()
{
	queue<node> q;
	memset(vis, 0, sizeof vis);
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			if (a[i][j] == '@')
			{
				q.push({i, j, 0});
				vis[i][j] = 1;
			}
	while (q.empty() == 0)
	{
		node cur = q.front();
		q.pop();
		if (cur.step == m)
			return;
		ans++;
		for (int i = 0; i < 4; i++)
		{
			int nx = cur.x + dx[i];
			int ny = cur.y + dy[i];
			if (nx <= 0 || ny <= 0 || nx > n || ny > n)
				continue;
			if (a[nx][ny] == '#')
				continue;
			if (vis[nx][ny] == 1)
				continue;
			vis[nx][ny] = 1;
			q.push({nx, ny, cur.step + 1});
		}
	}
}
int main()
{
	cin >> n;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			cin >> a[i][j];
	cin >> m;
	bfs();
	cout << ans;
	return 0;
}

错题二:P2199 最后的迷宫

题意

  • 迷宫是 N 行 M 列,O 空地,X 墙。
  • 哈利可以在空地上上下左右移动,每步 1 秒。
  • 哈利视力很好,只要在某个空地,他能沿着八个方向(东、南、西、北、东北、东南、西南、西北)看到底,只要中间没墙,就能看到奖杯。
  • 奖杯位置固定,哈利起点固定,问:哈利先走到某个能直接看到奖杯的位置,再到奖杯(用飞来咒拿),最短需要多少秒?

思路

如果哈利站在 (x, y) 能看到奖杯 (ex, ey),那么从 (x, y) 出发到奖杯的时间 = dis[x][y](走到这的时间) + 0(因为直接飞来咒拿到,不用再走)。

所以我们要找所有能直接看到奖杯的位置,从中选一个 dis 最小的。

什么是“能看到奖杯”

从奖杯坐标出发,沿着 8 个方向一直走,直到碰到墙或边界,途中经过的所有空地,都能看到奖杯。

所以:

  1. 先 BFS 从哈利起点出发,算出到每个空地的最短步数 dis(上下左右移动)。
  2. 再从奖杯位置出发,沿 8 个方向“射线”式扩展,收集所有能看到奖杯的空地位置。
  3. 在这些位置里找 dis 的最小值,就是答案。
  4. 如果找不到(所有能看见奖杯的位置都不可达),输出 Poor Harry。

代码

C++
#include <bits/stdc++.h>
using namespace std;
#define N 16385
const int INF = 0x3f3f3f3f;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
const int ddx[] = {0, 0, 1, 1, 1, -1, -1, -1};
const int ddy[] = {1, -1, -1, 0, 1, -1, 0, 1};
int sx, sy, ex, ey;
string s[N];
vector<int> dis[N];
struct node
{
    int x, y;
} ;
int n, m;
queue <node> q;
bool check(int x, int y)
{
    return x >= 0 && x < n && y >= 0 && y < m && s[x][y] == 'O';
}
void bfs(int sx, int sy)
{
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            dis[i][j] = INF;
    q.push(node{sx, sy});
    dis[sx][sy] = 0;
    while (!q.empty())
    {
        int x = q.front().x;
        int y = q.front().y;
        q.pop();
        for (int i = 0; i < 4; i++)
        {
            int nx = x + dx[i];
            int ny = y + dy[i];
            if (check(nx, ny) && dis[nx][ny] == INF)
            {
                dis[nx][ny] = dis[x][y] + 1;
                q.push({nx, ny});
            }
        }
    }
}
int main()
{
    cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        cin >> s[i];
        dis[i].resize(m + 1);
    }
    while (cin >> ex >> ey >> sx >> sy)
    {
        if (!ex && !ey && !sx && !sy)
            break;
        ex--, ey--, sx--, sy--;
        bfs(sx, sy);
        int ans = dis[ex][ey];
        for (int i = 0; i < 8; i++)
        {
            int x = ex, y = ey;
            while (check(x + ddx[i], y + ddy[i]))
            {
                x += ddx[i];
                y += ddy[i];
                ans = min(ans, dis[x][y]);
            }
        }
        if (ans == INF)
            cout << "Poor Harry\n";
        else
            cout << ans << "\n";
    }
    return 0;
}

错题三:P1649 [USACO07OCT] Obstacle Course S

题意

  • 地图 N × N,. 空地,x 障碍,A 起点,B 终点。
  • 牛只能朝上下左右方向走,她笨,不想拐弯,要求最少的 90° 转弯次数。
  • 可以任意方向开始,可以任意方向结束。

思路

  • 普通 BFS 只是最短步数,但这里要求最少转弯次数,所以要用分层 BFS: 同一个方向走,转弯次数不变;一旦换方向,转弯次数 +1。

  • 我们可以用一个 vis[x][y] 存到 (x, y) 的最小转弯次数(初始 INF)。

  • 然后 BFS 队列里还要存 当前方向(dx, dy)。

  • 入队新格子时:

    • 如果方向没变 → 转弯次数不变
    • 如果方向变了 → 转弯次数 +1
  • 如果新转弯次数 ≤ vis[nx][ny],就更新,并把它入队(因为转弯次数少或相同都要继续走,可能之后会到达 B 更新更优解)。

代码

C++
#include <bits/stdc++.h>
using namespace std;
int n, sx, sy, ex, ey;
char a[105][105];
int vis[105][105];
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
struct node
{
    int x, y, step, dx, dy;
} ;
void bfs(int sx, int sy, int dxi, int dyi)
{
    queue <node> q;
    vis[sx][sy] = 0;
    q.push({sx, sy, 0, dxi, dyi});
    while (q.size())
    {
        node t = q.front();
        q.pop();
        for (int i = 0 ; i < 4; i++)
        {
            int nx = t.x + dx[i];
            int ny = t.y + dy[i];
            int nstep = t.step;
            if (dx[i] != t.dx || dy[i] != t.dy)
                nstep++;
            if (nx < 1 || nx > n || ny < 1 || ny > n || a[nx][ny] == 'x')
                continue;
            if (nstep <= vis[nx][ny])
            {
                vis[nx][ny] = nstep;
                q.push({nx, ny, nstep, dx[i], dy[i]});
            }
        }
    }
    return ;
}
int main()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
        {
            cin >> a[i][j];
            if (a[i][j] == 'A')
                sx = i, sy = j;
            if (a[i][j] == 'B')
                ex = i, ey = j;
        }
    memset(vis, 0x3f, sizeof vis);
    for (int i = 0; i < 4; i++)
        bfs(sx, sy, dx[i], dy[i]);
    if (vis[ex][ey] == 0x3f3f3f3f)
        cout << -1;
    else
        cout << vis[ex][ey];
	return 0;
}
原文