POJ 3669 小坑

来源:互联网 发布:ftp服务器端口默认为 编辑:程序博客网 时间:2024/05/02 02:04

Description
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.

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

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

Sample Input
4
0 0 2
2 1 2
1 1 2
0 3 5
Sample Output
5

题目:
一个人要在一大块平地上走,有一些是安全的,有一些是不能走的,流星炸过的区域是不能走的,即这个点的上下左右,每个点给出了一个时间,在这个时间之前是可以走的,之后就不能走,所以模拟一下t, 大于等于这个时间就不走。如果两个流星炸了同一块地,这块地损坏的时间要选早的那个, 给出的流星轰炸的范围是 (0,0) -》 (300, 300) 这不代表这个人的行动范围就限制在这里,如果全部轰炸的话,这个人还是能走到安全的地方的 走到 302 之后就行。最后这个人要走的安全的那块地在这个时刻必须上下左右都是安全的。

#include <iostream>#include <cstring>#include <string>#include <algorithm>#include <cstdio>#include <vector>#include <cmath>#include <queue>using namespace std;const int INF = 0x3f3f3f3f;int g[310][310];int dirx[5] = {-1, 0, 0, 1};int diry[5] = {0, 1, -1, 0};int dis[310][310];int vis[310][310];class Point{public:    int x, y;    int d;    Point(int xx = 0, int yy = 0, int dd = 0) : x(xx), y(yy), d(dd) {}};int bfs(){    memset(vis, 0, sizeof vis);    vis[0][0] = 1;    queue<Point > q;    q.push(Point(0, 0, 0));    while(!q.empty())    {        Point p = q.front();        q.pop();        for(int i = 0; i < 4; i++)        {            int xx = p.x + dirx[i];            int yy = p.y + diry[i];            if(xx >= 0 && xx < 310 && yy >= 0 && yy < 310 && vis[xx][yy] == 0)//要0几以上的数, 保证他可以走到安全的地方。            {                if(g[xx][yy] == INF)                    return p.d+1;                else                    {                        if(p.d + 1 < g[xx][yy])                        {                            vis[xx][yy] = 1;                            q.push(Point(xx, yy, p.d+1));                        }                    }            }        }    }    return -1;}int main(){    int n;    while(scanf("%d", &n) != EOF)    {        for(int i = 0; i < 310; i++)            for(int j = 0; j < 310; j++)                g[i][j] = INF;        int x, y, t;        for(int i = 0; i < n; i++)        {            scanf("%d%d%d", &x, &y, &t);            g[x][y] = min(g[x][y], t);            for(int j = 0; j < 4; j++)//给轰炸的地方做 时间t 标记            {                int xx = x + dirx[j];                int yy = y + diry[j];                if(xx >= 0 && xx < 310 && yy >= 0 && yy < 310)                {                    g[xx][yy] = min(g[xx][yy], t);//同一个地方选时间小的                }            }        }        int a = bfs();        printf("%d\n", a);    }    return 0;}
0 0
原创粉丝点击