admin管理员组

文章数量:1532344

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.

输入输出样例

输入 #1

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

输出 #1

5
题意:在一个农场里,有M个流星落下来,第i个流星,坐标为(xi,yi),下落时间为ti, 流星下落时会砸到5个点,自己的点,该点的上下左右点。砸下来之后,贝西不能走这5个点。但是在砸下来之前可以走。
思路:流星下落时间进行排序,早的在前,然后把下落的时间放进一个dangerous数组,注意如果一个格子同时被多个流星影响到,考虑最早的格子。然后bfs

#include <iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;

const int N = 50000 + 100;//最大流星个数
struct star
{
    int x, y, t;//坐标以及落地时间
} a[N], b[N];

struct point
{
    int x, y, step;//逃亡到达的点坐标,所用的时间
};

bool cmp(const star a, const star b)
{
    return a.t < b.t;//时间小的在前
}

int M, MaxTime;
int dangerous[500][500];//第k秒之后有危险
int step[500][500] = {-1};//-1表示当前格子未被访问

int bfs(int x, int y)
{
    queue<point>q;
    q.push((point){x, y, 0});
    int p = 0;//指向数组当前的位置
    while(!q.empty())
    {
        point cur = q.front();//队首位置
        q.pop();
        if(step[cur.x][cur.y] != -1)continue;
        step[cur.x][cur.y] = cur.step;
        if(dangerous[cur.x][cur.y] == 1010)//当前位置没危险
            return cur.step;

        int ne[4][2] = {{-1, 0}, {1, 0}, {0, - 1}, {0, 1}};
        for(int i = 0; i < 4; i++)
        {
            int dx = cur.x + ne[i][0];
            int dy = cur.y + ne[i][1];
            //不走出去,在流星掉下来之前走过去,还没走过
            if(dx >= 0 && dx <= 302 && dy >= 0 && dy <= 302 && dangerous[dx][dy] > cur.step + 1 && step[dx][dy] == -1)
            {
                q.push((point){dx, dy, cur.step + 1});
            }
        }
    }
    return -1;
}

int main()
{
    scanf("%d", &M);
    for(int i = 0; i < M; i++)
    {
        scanf("%d%d%d", &b[i].x, &b[i].y, &b[i].t);
    }
    sort(b, b + M, cmp);//按时间对陨石进行排序
    int j = 1;
    a[0] = b[0];//把b[0]给a[0]
    for(int i = 1; i < M; i++)
    {
        if(a[j].x != b[i].x || a[j].y != b[i].y || a[j].t != b[i].t)
        {
            a[j++] = b[i];
        }
    }
    M = j;
    MaxTime = a[M-1].t;
    

    for(int i = 0; i <= 302; i++)
    {
        for(int j = 0; j <= 302; j++)
        {
            dangerous[i][j] = 1010;
            step[i][j] = -1;
        }
    }
    for(int i = 0; i < M; i++)
    {
        int x = a[i].x, y = a[i].y;
        if(dangerous[x][y] == 1010)//未被之前的流星影响到
            dangerous[x][y] = a[i].t;
        if(x - 1 >= 0 && dangerous[x - 1][y] == 1010)//不在范围内 未被之前的流星影响到
            dangerous[x - 1][y] = a[i].t;
        if(x + 1 <= 300 && dangerous[x + 1][y] == 1010)
            dangerous[x + 1][y] = a[i].t;
        if(y - 1 >= 0 && dangerous[x][y - 1] == 1010)
            dangerous[x][y - 1] = a[i].t;
        if(y + 1 <= 300 && dangerous[x][y + 1] == 1010)
            dangerous[x][y + 1] = a[i].t;
    }
    printf("%d", bfs(0,0));
    return 0;

}

本文标签: 洛谷P2895Meteorshower