Loading

洛谷P2895 [USACO08FEB]Meteor Shower S(BFS)

题目描述

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.

题意翻译

贝茜听说了一个骇人听闻的消息:一场流星雨即将袭击整个农场,由于流星体积过大,它们无法在撞击到地面前燃烧殆尽,届时将会对它撞到的一切东西造成毁灭性的打击。很自然地,贝茜开始担心自己的安全问题。以FJ牧场中最聪明的奶牛的名誉起誓,她一定要在被流星砸到前,到达一个安全的地方(也就是说,一块不会被任何流星砸到的土地)。如果将牧场放入一个直角坐标系中,贝茜现在的位置是原点,并且,贝茜不能踏上一块被流星砸过的土地。 根据预报,一共有M颗流星(1 <= M <= 50,000)会坠落在农场上,其中第i颗流星会在时刻T_i (0 <= T_i <= 1,000)砸在坐标为(X_i, Y_i) (0 <= X_i <= 300;0 <= Y_i <= 300)的格子里。流星的力量会将它所在的格子,以及周围4个相邻的格子都化为焦土,当然贝茜也无法再在这些格子上行走。

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

请你计算一下,贝茜最少需要多少时间才能到达一个安全的格子。

Translated by @跪下叫哥

输入输出样例

输入 #1
4
0 0 2
2 1 2
1 1 2
0 3 5
输出 #1
5
看到求最小值就知道是BFS,麻烦的地方在于时间的处理。可以这么写:首先如果一个地方终究是要被砸到的话先给其打上标记,然后用数组存储该处何时不可通过。一定要注意,如果一个点被重复覆盖的话,所有不可通过的时间里要取最小的(一开始直接用时间是否为0判断能不能走等等,后来发现有的地方被烧焦的时间为0)。然后就是常规BFS,判断的时候mark[nx][ny]==0||(mark[nx][ny]!=0&&pre.t+1<mmap[nx][ny])表示这个格子安全或者当前可通行。
注意:坑点在于x,y<=300这个范围是指流星袭击的范围而不是边界,边界判断的话不能卡在300,而应该比300大,实测315可以过,最后一个点输出-1而WA的就是这个原因。
#include <bits/stdc++.h>
using namespace std;
int mmap[315][315]={0};//为0说明没有流星 不为0说明流星在该时刻砸到这里 
bool mark[315][315]={0};//只要会被袭击就为1 
bool vis[315][315]={0};
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
int n;
struct node
{
    int x;
    int y;
    int t;
};
void bfs()
{
    node start;
    start.x=0;
    start.y=0;
    start.t=0;
    queue<node>q;
    q.push(start);
    vis[0][0]=1;
    while(q.size())
    {
        node pre=q.front();
        q.pop();
        if(mmap[pre.x][pre.y]==0)
        {
            cout<<pre.t;
            return;
        }
        int i;
        for(i=0;i<4;i++)
        {
            int nx=dir[i][0]+pre.x;
            int ny=dir[i][1]+pre.y;
            if(!vis[nx][ny]&&nx>=0&&nx<=315&&ny>=0&&ny<=315&&(mark[nx][ny]==0||(mark[nx][ny]!=0&&pre.t+1<mmap[nx][ny])))
            {
                node nxt;
                nxt.x=nx;
                nxt.y=ny;
                nxt.t=pre.t+1;
                vis[nx][ny]=1;
                q.push(nxt);
            }
        }
    }
    cout<<-1;
}
int main()
{
    cin>>n;
    int i;
    for(i=1;i<=n;i++)
    {
        int t,x,y;
        scanf("%d%d%d",&x,&y,&t);
        mark[x][y]=1; 
        if(mmap[x][y])mmap[x][y]=min(mmap[x][y],t);//有可能有的在0s就爆炸了 
        else mmap[x][y]=t;//
        if(x-1>=0)
        {
            mark[x-1][y]=1;
            if(mmap[x-1][y])mmap[x-1][y]=min(mmap[x-1][y],t);
            else mmap[x-1][y]=t;//
        }//所有的t要取最近的一个时间 
        if(x+1<=315)
        {
            mark[x+1][y]=1;
            if(mmap[x+1][y])mmap[x+1][y]=min(mmap[x+1][y],t);
            else mmap[x+1][y]=t;//
        }
        if(y-1>=0)
        {
            mark[x][y-1]=1;
            if(mmap[x][y-1])mmap[x][y-1]=min(mmap[x][y-1],t);
            else mmap[x][y-1]=t;//
        }
        if(y+1<=315)
        {
            mark[x][y+1]=1;
            if(mmap[x][y+1])mmap[x][y+1]=min(mmap[x][y+1],t);
            else mmap[x][y+1]=t;//
        }    
    }
    bfs();
    return 0;
}

 

posted @ 2020-03-14 01:03  脂环  阅读(340)  评论(0编辑  收藏  举报