POJ 3669 Meteor Shower(流星雨)

POJ 3669 Meteor Shower流星雨

Time Limit: 1000MS    Memory Limit: 65536K


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.


Bessie听说有场史无前例的流星雨即将来临;有谶言:陨星将落,徒留灰烬。为保生机,她誓将找寻安全之所(永避星坠之地)。目前她正在平面坐标系的原点放牧,打算在群星断其生路前转移至安全地点。

此次共有M (1 ≤ M ≤ 50,000)颗流星来袭,流星i将在时间点Ti (0 ≤ Ti  ≤ 1,000) 袭击点 (Xi, Yi) (0 ≤ Xi ≤ 300; 0 ≤ Yi ≤ 300)。每颗流星都将摧毁落点及其相邻四点的区域。

Bessie在0时刻时处于原点,且只能行于第一象限,以平行与坐标轴每秒一个单位长度的速度奔走于未被毁坏的相邻(通常为4)点上。在某点被摧毁的刹那及其往后的时刻,她都无法进入该点。


Input


输入


* Line 1: A single integer: M

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


* 第1行: 一个整数: M

* 第2..M+1行: 第i+1行包含由空格分隔的三个整数: 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.


* 仅一行: Bessie寻得安全点所花费的最短时间,无解则为-1。


Sample Input - 输入样例


Sample Output - 输出样例

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

【题解】

从起点开始进行SPFA(DFS)即可。

需要注意的是在读取各个落点的数据时,只保留最早被毁灭的时间点。

【代码 C++】

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <queue>
 5 #define mx 305
 6 int dg[mx][mx], tim[mx][mx], drc[5][2] = { 1, 0, -1, 0, 0, 1, 0, -1, 0, 0 };
 7 struct Point{
 8     int y, x;
 9 }temp, nxt;
10 int main(){
11     int m, x, y, t, i, nowT, opt;
12     scanf("%d", &m);
13     memset(dg, -1, sizeof(dg));
14     while (m--){
15         scanf("%d%d%d", &x, &y, &t);
16         ++y; ++x;
17         for (i = 0; i < 5; ++i){
18             temp.y = y + drc[i][1]; temp.x = x + drc[i][0];
19             if (~dg[temp.y][temp.x]) dg[temp.y][temp.x] = std::min(dg[temp.y][temp.x], t);
20             else dg[temp.y][temp.x] = t;
21         }
22     }
23
24     memset(dg, 0, sizeof(dg[0])); memset(dg[mx - 1], 0, sizeof(dg[0]));
25     for (i = 0; i < mx; ++i) dg[i][0] = dg[i][mx - 1] = 0;
26     memset(tim, 127, sizeof(tim));
27     nowT = 0; opt = 2100000000;
28     std::queue<Point> q;
29     tim[1][1] = 0; temp.y = temp.x = 1; q.push(temp);
30     while (!q.empty()){
31         temp = q.front(); q.pop();
32         if (dg[temp.y][temp.x] == -1){
33             opt = std::min(opt, tim[temp.y][temp.x]);
34             continue;
35         }
36         for (i = 0; i < 4; ++i){
37             nxt.y = temp.y + drc[i][0]; nxt.x = temp.x + drc[i][1];
38             if (tim[temp.y][temp.x] + 1 < dg[nxt.y][nxt.x] || dg[nxt.y][nxt.x] == -1){
39                 if (tim[temp.y][temp.x] + 1 < tim[nxt.y][nxt.x]){
40                     tim[nxt.y][nxt.x] = tim[temp.y][temp.x] + 1;
41                     q.push(nxt);
42                 }
43             }
44         }
45     }
46     if (opt == 2100000000) puts("-1");
47     else printf("%d", opt);
48     return 0;
49 }
时间: 2024-12-26 00:01:46

POJ 3669 Meteor Shower(流星雨)的相关文章

POJ 3669 Meteor Shower【BFS】

POJ 3669 去看流星雨,不料流星掉下来会砸毁上下左右中五个点.每个流星掉下的位置和时间都不同,求能否活命,如果能活命,最短的逃跑时间是多少? 思路:对流星雨排序,然后将地图的每个点的值设为该点最早被炸毁的时间 #include <iostream> #include <algorithm> #include <queue> #include <cstring> using namespace std; #define INDEX_MAX 512 int

poj 3669 Meteor Shower(bfs)

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 destroy

poj 3669 Meteor Shower

Meteor Shower Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16339   Accepted: 4293 Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteors will crash into earth and destroy anything they h

POJ 3669 Meteor shower 简单BFS, 有坑点

Meteor Shower 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

POJ 3669 Meteor Shower (BFS + 预处理)

Meteor Shower Time Limit: 1000MS   Memory Limit: 65536K Total 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 hi

题解报告:poj 3669 Meteor Shower(bfs)

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 destroy

POJ 3669 Meteor Shower 题解 《挑战程序设计竞赛》

题目链接: http://poj.org/problem?id=3669 题意: 这个同学发现流星雨要来了,会对地球造成伤害于是决定逃跑.N颗流星会不定时降落在坐标轴第一象限300*300内的点上.给出每颗流星降落的坐标和时刻,求这个同学能否成功逃跑,能的话用时多少. 思路: 略有一点tricky,无法变通了(@﹏@)~.看了码农场的代码. 这道题没有给出地图,所以需要自己生成地图. 关键的点在于:在生成地图的时候,将每个点的初始值设为无穷大(表示不会有流星),将要被摧毁的点设置为流星降落的时刻

【POJ 3669 Meteor Shower】简单BFS

流星雨撞击地球(平面直角坐标第一象限),问到达安全地带的最少时间. 对于每颗流星雨i,在ti时刻撞击(xi,yi)点,同时导致(xi,yi)和上下左右相邻的点在ti以后的时刻(包括t)不能再经过(被封锁).安全地带为永远不会被封锁的点. 简单bfs,开始WA在把平面空间上限当成300*300,但根据题目,这只是有流星雨撞击的范围.实际可走的空间理论上没上限,但分析可得,离原点最近的安全地带一定在(302,302)范围内,所以应可把数组至少开为303*303. 后来WA在把G[0][0]==1的情

POJ - 3669 Meteor Shower(bfs)

题意:某人在时刻0从原点出发,在第一象限范围内移动.已知每个炸弹爆炸的地点和时刻,炸弹爆炸可毁坏该点和它上下左右的点.不能经过已毁坏的点,也不能在某点毁坏的时候位于该点.不会被炸弹毁坏的地方是安全之处,问某人到达安全之处的最短时间. 分析:bfs即可.注意:1.某点多次爆炸或受爆炸点影响而毁坏,取最早时间为该点的毁坏时间.2.bfs走过的点不能再走,注意标记. #pragma comment(linker, "/STACK:102400000, 102400000") #include