P2895 [USACO08FEB]Meteor Shower S——bfs

发布于:2022-11-02 ⋅ 阅读:(424) ⋅ 点赞:(0)

[USACO08FEB]Meteor Shower S

题面翻译

贝茜听说了一个骇人听闻的消息:一场流星雨即将袭击整个农场,由于流星体积过大,它们无法在撞击到地面前燃烧殆尽,届时将会对它撞到的一切东西造成毁灭性的打击。很自然地,贝茜开始担心自己的安全问题。

以 Farmer John 牧场中最聪明的奶牛的名誉起誓,她一定要在被流星砸到前,到达一个安全的地方(也就是说,一块不会被任何流星砸到的土地)。

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

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

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

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

Translated by @奆奆的蒟蒻 @跪下叫哥

题目描述

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

分析

  1. 把所有点都预处理一下,a数组存每个坐标被流星摧毁的时刻,先全初始为-1(表示没被摧毁是安全点),然后逐个把流星砸来的点,以及扩散的点,将时刻t赋给数组,由于流星可能会在不同的时刻砸向同一个点,流星砸下时间以最早的那个为准
  2. 由于是找最短路问题,就从起点开始广搜,然后bfs中遇到某个点a[xx][yy] == -1 就说明找到了安全点,直接保存答案,return;
  3. 需要注意输入的顺序是x,y,t,刚开始默认以为中文题意的t,x,y的顺序,然后发现样例不对劲;
  4. 可能起始点(0,0)就是安全地,所以应该在出队时候也特判下,不过测试样例没这个,也可以不加就可以过,但为了有的题卡测试点,还是考虑全面点加上;
  5. 样例的解析如下图,格子的值为流星会摧毁该坐标点的时刻;

在这里插入图片描述

#include<bits/stdc++.h>

using namespace std;

struct node {
    int x, y, step;

    node(int xx, int yy, int ste) {
        x = xx, y = yy, step = ste;
    }
};

int m, x, y, t, ans = -1;
int a[305][305];
int vis[305][305];
queue<node> q;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};

void bfs() {
    q.push(node(0, 0, 0));
    vis[0][0] = 1;
    while (!q.empty()) {
        node no = q.front();
        q.pop();
        //可能起始点0,0就是安全地,所以这里应该也特判下,不过测试样例没这个,也可以不加
        if (a[no.x][no.y] == -1) {
            ans = no.step;
            return;
        }
        for (int i = 0; i < 4; i++) {
            int xx = no.x + dx[i];
            int yy = no.y + dy[i];
            //到达安全地
            if (a[xx][yy] == -1 && xx >= 0 && yy >= 0) {
                ans = no.step + 1;
                return;
            }
            //(xx,yy)不能被走过,且到达它时,此时没流星
            if (!vis[xx][yy] && xx >= 0 && yy >= 0 && a[xx][yy] > no.step + 1) {
                q.push(node(xx, yy, no.step + 1));
                vis[xx][yy] = 1;
            }
        }
    }
}

int main() {
    cin >> m;
    memset(a, -1, sizeof a);
    //预处理整个地图
    while (m--) {
        cin >> x >> y >> t;
        //流星砸下时间以最早的那个为准
        if (a[x][y] == -1 || a[x][y] > t)
            a[x][y] = t;
        for (int i = 0; i < 4; i++) {
            int xx = x + dx[i];
            int yy = y + dy[i];
            if (xx >= 0 && yy >= 0 && (a[xx][yy] == -1 || a[xx][yy] > t))
                a[xx][yy] = t;
        }
    }
    bfs();
    cout << ans;
    return 0;
}

本文含有隐藏内容,请 开通VIP 后查看