每日一题4.14 P2895 [USACO08FEB]Meteor Shower

P2895 [USACO08FEB]Meteor Shower S

题目描述

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.

题意翻译

贝茜听说一场非凡的流星雨即将来临;
有报道称,这些流星会撞向地球,并摧毁它们撞到的任何东西。
由于担心自己的安全,她发誓要找到一个安全的地方(一个不会被流星摧毁的地方)。
她目前正在坐标平面上的原点上吃草,想要移动到一个新的、更安全的地方,同时避免被沿途的流星摧毁。
报告说M颗流星(1 M 5万颗)将会撞击,与流星i将会撞击点(Xi, Yi) (0 Xi 300;
0 Yi 300)时Ti (0 Ti 1000)。
每颗流星都会摧毁它所撞击的点,以及四个直线相邻的晶格点。
贝西在时间0离开原点,并在第一象限以每秒钟1单位的速度与坐标轴平行移动到(通常是4个)尚未被流星摧毁的相邻直线点。
她不能在任何大于或等于它被摧毁的时间点上被找到)。
确定贝茜到达安全地点的最短时间。

反手就是机翻

输入输出样例

输入 #1

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

输出 #1

5

题解

真是有趣

这题一看是老BFS了

更有趣的是

我忘了BFS咋写了,漂亮!

复习一波。。。

学习回来。。。

现在,马上天上要掉下来一坨坨要命的东西,小贝茜想保证自己的生命,作为预言家的我们,提前知道了哪些地方要落下奇怪的东西,并且落下后小贝茜不能踏入那块地方以及其周边的地方。现在,我们可以通过(计算机)精密的计算,得知小贝茜怎么能最轻易的活下来。

有没有觉得输入看不懂?以为是个图? = = 其实是输入横纵坐标和落下时间的。。。就我一个人看迷糊了半天?!

小贝茜完蛋要输出-1,从dalao们的题解中得知。。。我咋在哪都没看到???

#include <bits/stdc++.h>
using namespace std;
struct node{
    int x1,y1,time;
};
queue<node> bfs;
int dx[5] = {1,0,0,-1,0};
int dy[5] = {0,1,-1,0,0};
int mp[500][500];
bool check[500][500];
int n;
int x,y,tt;
int main()
{
    cin >> n;
    memset(mp,-1,sizeof(mp));
    for(int i = 1;i <= n;i++){
        cin >> x >> y >> tt;
        for(int j = 0;j < 5;j++){
            if(x + dx[j] < 0 || y + dy[j] < 0){
                    continue;
            }
            if(mp[x + dx[j]][y + dy[j]] != -1){
                mp[x + dx[j]][y + dy[j]] = min(mp[x + dx[j]][y + dy[j]],tt);
            }
            else{
                mp[x + dx[j]][y + dy[j]] = tt;
            }
        }
    }
    bfs.push({0,0,0});
    check[0][0] = 1;
    while(!bfs.empty()){
        node head = bfs.front();
        if(mp[head.x1][head.y1] == -1){

            cout << head.time<<endl;
            return 0;
        }
        bfs.pop();
        for(int i = 0;i < 4;i++){
            int tx = head.x1 + dx[i],ty = head.y1 + dy[i];
            if(tx < 0||ty < 0||check[tx][ty] == 1)continue;
            if(head.time >= mp[tx][ty] - 1 && mp[tx][ty] != -1)continue;
            check[tx][ty] = 1;
            bfs.push({tx,ty,head.time + 1});
        }
    }
    cout << -1;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值