洛谷 P2895. Meteor Shower S -- 一道好题 利用bfs搜索模板 但特殊 也有坑

[USACO08FEB] Meteor Shower S

题面翻译

题目描述

贝茜听说一场特别的流星雨即将到来:这些流星会撞向地球,并摧毁它们所撞击的任何东西。她为自己的安全感到焦虑,发誓要找到一个安全的地方(一个永远不会被流星摧毁的地方)。

如果将牧场放入一个直角坐标系中,贝茜现在的位置是原点,并且,贝茜不能踏上一块被流星砸过的土地。

根据预报,一共有 \(M\) 颗流星 \((1\leq M\leq 50,000)\) 会坠落在农场上,其中第 \(i\) 颗流星会在时刻 \(T_i\)\(0 \leq T _ i \leq 1000\))砸在坐标为 \((X_i,Y_i)(0\leq X_i\leq 300\)\(0\leq Y_i\leq 300)\) 的格子里。流星的力量会将它所在的格子,以及周围 \(4\) 个相邻的格子都化为焦土,当然贝茜也无法再在这些格子上行走。

贝茜在时刻 \(0\) 开始行动,她只能在第一象限中,平行于坐标轴行动,每 \(1\) 个时刻中,她能移动到相邻的(一般是 \(4\) 个)格子中的任意一个,当然目标格子要没有被烧焦才行。如果一个格子在时刻 \(t\) 被流星撞击或烧焦,那么贝茜只能在 \(t\) 之前的时刻在这个格子里出现。 贝茜一开始在 \((0,0)\)

请你计算一下,贝茜最少需要多少时间才能到达一个安全的格子。如果不可能到达输出 \(−1\)

输入格式

\(M+1\) 行,第 \(1\) 行输入一个整数 \(M\),接下来的 \(M\) 行每行输入三个整数分别为 \(X_i, Y_i, T_i\)

输出格式

贝茜到达安全地点所需的最短时间,如果不可能,则为 \(-1\)

题目描述

Bessie hears that an extraordinary meteor shower is coming; reports say that these meteors will crash into earth and destroy anything they hit. Anxious for her safety, she vows to find her way to a safe location (one that is never destroyed by a meteor) . She is currently grazing at the origin in the coordinate plane and wants to move to a new, safer location while avoiding being destroyed by meteors along her way.

The reports say that M meteors (1 ≤ M ≤ 50,000) will strike, with meteor i will striking point (Xi, Yi) (0 ≤ Xi ≤ 300; 0 ≤ Yi ≤ 300) at time Ti (0 ≤ Ti  ≤ 1,000). Each meteor destroys the point that it strikes and also the four rectilinearly adjacent lattice points.

Bessie leaves the origin at time 0 and can travel in the first quadrant and parallel to the axes at the rate of one distance unit per second to any of the (often 4) adjacent rectilinear points that are not yet destroyed by a meteor. She cannot be located on a point at any time greater than or equal to the time it is destroyed).

Determine the minimum time it takes Bessie to get to a safe place.

输入格式

* Line 1: A single integer: M

* Lines 2..M+1: Line i+1 contains three space-separated integers: Xi, Yi, and Ti

输出格式

* Line 1: The minimum time it takes Bessie to get to a safe place or -1 if it is impossible.

样例 #1

样例输入 #1

4
0 0 2
2 1 2
1 1 2
0 3 5

样例输出 #1

5

题解

在那想半天该用什么方式存陨石坐标和下落时间map queue都试了一遍
实际上就用一个二维数组存不就行了吗 g[x][y] = t 这样不就把一对坐标和时间都给存下来了吗 太fw了
本题坑的地方还是比较多的 你需要对陨石烧焦的地面列一个地图
本题退出的条件并不是把所有点都走过队列清空
而是 走到安全点 所以每一次循环你都要判断是否走到了安全点
安全点就是永远不会被陨石烧焦的地面 所以我们还要对所有地面初始化
由于流星可能砸到从(0,0)到(300,300)的所有坐标,所以有可能走到300外的点,开数组的时候不能开小了
一道好题

#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> PII;

const int N = 50010, M = 310; //M要开的比300大

int dx[5] = {0, 0, 0, 1, -1}, dy[5] = {0, 1, -1, 0, 0}; //多存了个自己的原坐标 

int n, x, y, t;
int g[M][M];
bool st[M][M]; //存这个点有没有被烧焦 
bool ju[M][M]; //存走过的点
int ans[M][M];

void solve()
{
    scanf("%d", &n);
    memset(g, 0x3f, sizeof g); //初始化陨石地图每个点为inf 在后面判断该点到底会不会有陨石坠落有大用处 
    for (int i = 1; i <= n; i ++ )
    {
        scanf("%d%d%d", &x, &y, &t);
        //关键点 超级易错!
        //由于烧焦的格子不能走进 并且 陨石可能降落在同一个位置 那我们就以最早坠落的为准 因为只要烧焦了就不能进 则越早烧焦越好
        for (int j = 0; j < 5; j ++ )
        {
            //所以if里有两种判断 成立一种 就可以进入if 1 当前点没有炸过进去炸 2 该点炸过了 但是该点之前输入的炸的时间比当前时间大 用当前时间覆盖进去炸 因为越早炸越好 
            if ((!st[x + dx[j]][y + dy[j]] && (x + dx[j] >= 0) && (y + dy[j] >= 0)) || (t < g[x + dx[j]][y + dy[j]] && (x + dx[j] >= 0) && (y + dy[j] >= 0)))
            {
                st[x + dx[j]][y + dy[j]] = true;
                g[x + dx[j]][y + dy[j]] = t; //只有该点没有搜过 或者 新的时间比旧的时间小 采用当前时间覆盖原来的值 
            }
        }
    }

    //把陨石坠落的位置和时间预处理完后就可以开始走了
    queue<PII> q;
    q.push({0, 0}); 
    while (q.size())
    {
        auto t = q.front();
        q.pop();
        int a = t.first, b = t.second;
        if (g[a][b] == 0x3f3f3f3f) //如果该点根本不会被烧焦 直接输出走到该点的时间 输出 这里加是防止(0, 0)不会被烧焦 导致走都不用走的特殊情况出现 
        {
            printf("%d\n", ans[a][b]);
            return;
        }
        for (int i = 1; i < 5; i ++ ) //遍历当前点可能走到的点 
        {

            int l = a + dx[i], r = b + dy[i];
            if ( !ju[l][r] && g[l][r] > ans[a][b] + 1 && l >= 0 && r >= 0) //如果走到该点时该点还没有被烧焦 
            {
                ju[l][r] = true; //这里看似不需要判断该点有没有走过 因为我们只需要看该点当前时间点会不会被烧焦 但我们需要降低时间复杂度 不能让一个点重复入队 反正要是该点时安全点 我们下面的判断就直接退出循环了 
                ans[l][r] = ans[a][b] + 1; //该点在上一点走的基础上加1 
                if (g[l][r] == 0x3f3f3f3f)
                {
                    printf("%d\n", ans[l][r]);
                    return;
                }
                q.push({l, r});
            }
        }
    }

    printf("-1");
    return;
}

int main()
{
    solve();

    return 0;
}
posted @ 2024-04-16 16:05  MsEEi  阅读(46)  评论(0)    收藏  举报