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

· · 个人记录

考试情况

A B C D E 总分
0 100 8 20 0 128

错题一:T759360 流感传染

题意

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

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

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

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

思路

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

代码

#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 最后的迷宫

题意

思路

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

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

什么是“能看到奖杯”

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

所以:

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

代码

#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

题意

思路

代码

#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;
}