5.9 <407>总结

· · 个人记录

前言

依旧第5。 。 。

别问我为什么打省略号因为我166分

P4328

题意

有一个R行C列的地图,地图有这些东东:

可以移动到四个相邻区域(上、下、左、右)。每一分钟,淹没的区域就会向四方扩散,问从起点到河狸的巢穴的最短时间。注:不能移动到即将在同一分钟被淹没的区域!

思路

这个肯定要用bfs吧!最短时间我们可以看成最短步数。

可好像很难,又要记录洪水,又要记录你到的地方,一个是麻烦,一个是你可能做不到其他地方都同时扩散除非你想评测点眼前一片黑。

那怎么办呢?为什么要一个bfs,麻烦自己你又何必呢?你搞两个bfs不香吗?
那问题就很简单了,一个bfs来记录洪水扩延到每个点的时间,另一个则就是板子,只不过看看有没有洪水就行了。

错因

我忘记了河狸的巢穴不会被洪水淹没,然后sum数组的初始化初始成了-1(因为初始成-1的话,没有淹没的就会被程序误认为是被淹没的)。

AC代码

#include <bits/stdc++.h>
using namespace std;

const int N = 55;
int n, m;//我设n、m没关系吧
char a[N][N];
int sum[N][N];//洪水淹没(i,j)的时间
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 <= m; j ++)
            if(a[i][j] == '*')//这个点本身就被淹没
            {
                sum[i][j] = 0;
                q.push({i, j, 0});
            }
    while(q.empty() == 0)
    {
        node cur = q.front();
        q.pop();
        sum[cur.x][cur.y] = cur.step;
        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 > m)
                continue;
            if(a[nx][ny] == 'X' || a[nx][ny] == 'D')//不要忘了这个!!!
                continue;
            if(vis[nx][ny] == 1)
                continue;
            vis[nx][ny] = 1;
            q.push({nx, ny, cur.step + 1});
        }
    }
}
int bfs2(int sx, int sy)//板子
{
    queue<node> q;
    memset(vis, 0, sizeof vis);
    q.push({sx, sy, 0});
    vis[sx][sy] = 1;
    while(q.empty() == 0)
    {
        node cur = q.front();
        q.pop();
        if(a[cur.x][cur.y] == 'D')
            return cur.step;
        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 > m)
                continue;
            if(a[nx][ny] == 'X')
                continue;
            if(sum[nx][ny] <= cur.step + 1)
                continue;
            if(vis[nx][ny] == 1)
                continue;
            vis[nx][ny] = 1;
            q.push({nx, ny, cur.step + 1});
        }
    }
    return -1;
}
int main()
{
    memset(sum, 0x3f, sizeof sum);//初始化
    int sx, sy;
    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] == 'S')
                sx = i, sy = j;
        }
    bfs();
    int tmp = bfs2(sx, sy);
    if(tmp == -1)
        cout << "KAKTUS";
    else cout << tmp;
    return 0;
}

P2199

我不行了,也太尊重原著了吧(表情复杂)

题意

哈利想用飞来咒直接拿到三强争霸赛的奖杯,可他必须能看到奖杯就猎奇在这里,他能看到东北,东,东南,南,西南……方向,他能往上下左右走,且每跑一步花1s,问他最短什么时候看到奖杯。

思路

首先,你肯定不能直接开始,然后每一步判断看没看到,肯定眼前一片黑。

我们可以换一种角度:我们就把终点及能看到它的都记录一下,然后bfs一下,走到了就退出。

具体的,还是看代码吧

哦,不对,我还有错因!稍等一下吧

错因

第一,我直接开了一个17005 \times 17005的数组。空间限制128MB,只能开5000万,召唤计算器算一下2亿多……
第二,我严重怀疑我T了。

AC代码

#include <bits/stdc++.h>
using namespace std;

const int N = 16500;
int n, m;
int sx, sy, ex, ey;
string a[N];
vector<bool> vis[N];
struct node
{
    int x, y, step;
};
queue<node> q;
int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};
int see_x[] = {0, 0, -1, 1, -1, 1, -1, 1};
int see_y[] = {-1, 1, 0, 0, -1, 1, 1, -1};
int bfs()//然后就是板子
{
    while(q.empty() == 0)
    {
        node cur = q.front();
        q.pop();
        if(cur.x == ex && cur.y == ey)
            return cur.step;
        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 >= m)
                continue;
            if(a[nx][ny] == 'X')
                continue;
            if(vis[nx][ny] == 1)
                continue;
            vis[nx][ny] = 1;
            q.push({nx, ny, cur.step + 1});
        }
    }
    return -1;
}
int main()
{
    cin >> n >> m;
    for(int i = 0; i < n; i ++)
        cin >> a[i];
    while(cin >> sx >> sy >> ex >> ey)
    {
        while(q.empty() == 0)
            q.pop();
        if(sx == 0 && sy == 0 && ex == 0 && ey == 0)
            break;
        sx --, sy --, ex --, ey --;//都string了
        for(int i = 0; i < n; i ++)
            vis[i].clear();//记得清空
        for(int i = 0; i < n; i ++)//初始化
            for(int j = 0; j < m; j ++)
                vis[i].push_back(0);
        int x = sx, y = sy;
        vis[x][y] = 1;//记得标记终点
        q.push({x, y, 0});
        for(int i = 0; i < 8; i ++)
        {
            x = sx, y = sy;
            x = x + see_x[i], y = y + see_y[i];
            while(x >= 0 && x < n && y >= 0 && y < m)
            {
                if(a[x][y] == 'X')//撞到墙了
                    break;
                vis[x][y] = 1;
                q.push({x, y, 0});
                x = x + see_x[i], y = y + see_y[i];
            }
        }
        int ans = bfs();
        if(ans == -1)
            cout << "Poor Harry\n";
        else cout << ans << "\n";
    }
    return 0;
}

P1649

题意

有一个N \times N的场地,字符x代表牛不能走的,字符.代表牛可以通过的。给出起点和终点:A和B,求A到B的最短拐弯次数。

思路

这道题的难点是不是就是拐弯次数?那怎么办呢?我们可以在结构体里多加一个元素:方向。

加了方向之后,这个问题就好办了。我们维护一个vis数组来存从A到点(i,j)的最少拐弯次数。我们可以把每个方向都bfs一遍,然后每一次看队头存的方向有没有与此时判的方向不同,若不同,则这次的拐弯次数加1,然后再与vis[i][j]比较。

错因

没思路。

AC 代码

#include <bits/stdc++.h>
using namespace std;

const int N = 105;
int n;
char a[N][N];
int vis[N][N];
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
struct node
{
    int x, y, fx, step;//添加一个方向
};
void bfs(int sx, int sy, int FX)
{
    queue<node> q;
    q.push({sx, sy, FX, 0});
    vis[sx][sy] = 0;//自己到自己肯定是0次了
    while(q.empty() == 0)
    {
        node cur = q.front();
        q.pop();
        for(int i = 0; i < 4; i ++)
        {
            int nx = cur.x + dx[i];
            int ny = cur.y + dy[i];
            int nstep = cur.step;//新的拐弯次数
            if(cur.fx != i)
                nstep ++;
            if(nx <= 0 || ny <= 0 || nx > n || ny > n)
                continue;
            if(a[nx][ny] == 'x')
                continue;
            if(nstep <= vis[nx][ny])
            {
                vis[nx][ny] = nstep;
                q.push({nx, ny, i, nstep});
            }
        }
    }
}
int main()
{
    int sx, sy, ex, ey;
    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, i);
    if(vis[ex][ey] >= 0x3f3f3f3f)
        cout << -1;
    else cout << vis[ex][ey];
    return 0;
}