POJ 3669 Meteor Shower (BFS + 预处理)

来源:互联网 发布:c语言不合法标识符 编辑:程序博客网 时间:2024/05/16 19:35

Meteor Shower
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 9677 Accepted: 2718

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 (XiYi) (0 ≤ X≤ 300; 0 ≤ Y≤ 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: XiYi, 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

40 0 22 1 21 1 20 3 5

Sample Output

5

Source

USACO 2008 February Silver




题意:方形地面上,从某一时刻开始下陨石,已知M块陨石,给出落地的位置及时间,每块陨石落到地面的时候,都会将当前点以及前后左右四个相邻点都损坏掉,损坏了就不安全了。某人从(0,0)位置开始,问能否用最短的步数逃到安全的地方。人移动一步需要时间1.


解析:通过对地面区域的预处理,然后再进行bfs。




AC代码:

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <cmath>#include <cstdlib>#include <ctime>#include <stack>using namespace std;#define INF 0x7fffffff#define LL long longconst int maxn = 1000 + 10;struct node{ int x, y, time; };                            //点结构体int g[maxn][maxn];                                         //图int dir[5][2] = {0, 0, 0, 1, 0, -1, 1, 0, -1, 0};          //方向数组int bfs(){    if(g[0][0] == 0) return -1;                            //从(0,0)开始搜    if(g[0][0] == -1) return 0;                            //起点安全,不用躲了    node tmp, now;    tmp.x = tmp.y = tmp.time = 0;    queue<node> q;    q.push(tmp);    while(!q.empty()){        now = q.front();        q.pop();        for(int i=0; i<5; i++){            tmp.x = now.x + dir[i][0], tmp.y = now.y + dir[i][1];            tmp.time = now.time + 1;            if(tmp.x < 0 || tmp.y < 0 || tmp.x >= maxn || tmp.y >= maxn) continue;            if(g[ tmp.x ][ tmp.y ] == -1) return tmp.time;            if(tmp.time >= g[ tmp.x ][ tmp.y ]) continue;            g[ tmp.x ][ tmp.y ] = tmp.time;            q.push(tmp);        }    }    return -1;}int main(){    #ifdef sxk        freopen("in.txt", "r", stdin);    #endif // sxk    int n, x, y, d;    while(scanf("%d", &n)!=EOF){        memset(g, -1, sizeof(g));        for(int i=0; i<n; i++){            scanf("%d%d%d", &x, &y, &d);            for(int j=0; j<5; j++){                                //预处理                int dx = x + dir[j][0], dy = y + dir[j][1];                if(dx < 0 || dx >= maxn || dy < 0 || dy >= maxn) continue;                g[dx][dy] = g[dx][dy] == -1 ? d : min(g[dx][dy], d);                 }        }        printf("%d\n", bfs());    }    return 0;}




0 0
原创粉丝点击