您的位置:首页 > 其它

POJ 3669 Meteor Shower

2016-09-10 16:26 344 查看
Meteor Shower

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 16015Accepted: 4216
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
**思路,广搜,下面是剽窃大牛的题解

Sample Input

4 0 0 2 2 1 2 1 1 2 0 3 5
INPUT DETAILS:
There are four meteors, which strike points (0, 0); (2, 1); (1, 1); and (0, 3) at times 2, 2, 2, and 5, respectively.

t = 0 t = 2 t = 5
5|. . . . . . . 5|. . . . . . . 5|. . . . . . .
4|. . . . . . . 4|. . . . . . . 4|# . . . . . . * = meteor impact
3|. . . . . . . 3|. . . . . . . 3|* # . . . . .
2|. . . . . . . 2|. # # . . . . 2|# # # . . . . # = destroyed pasture
1|. . . . . . . 1|# * * # . . . 1|# # # # . . .
0|B . . . . . . 0|* # # . . . . 0|# # # . . . .
-------------- -------------- --------------
0 1 2 3 4 5 6 0 1 2 3 4 5 6 0 1 2 3 4 5 6

Sample Output

5

OUTPUT DETAILS:
Examining the plot above at t=5, the closest safe point is (3, 0) -- but Bessie's path to that point is too quickly blocked off by the second meteor. The next closest point is (4,0) -- also blocked too soon. Next closest after that are lattice points on the
(0,5)-(5,0) diagonal. Of those, any one of (0,5), (1,4), and (2,3) is reachable in 5 timeunits.

5|. . . . . . .
4|. . . . . . .
3|3 4 5 . . . . Bessie's locations over time
2|2 . . . . . . for one solution
1|1 . . . . . .
0|0 . . . . . .
--------------
0 1 2 3 4 5 6

大意就是说她如果想活命,最少要逃多少秒,一秒位移一次
先可以把数组初始化为 -1 表示安全的
然后有流星的地方赋值成对应的时间map[x][y] = t;一个点可能被多次摧毁,取最小值min(a,b)
这样我们就可以从(0,0)开始,用广搜找到 最进的一个 -1(安全地点)
注意两个特殊数据
1,在0秒时原点及其附近的流星,这是秒死,返回 -1;
2,原点上来就是 -1,表明原点就是安全的,返回最短时间 0;

读入和输出用scanf和printf,不然容易超时
还要要避免走过的路重新走,这样也会超时的
还有一点是流星的范围是[0,300],那么它破坏的最大范围可以到达301,而她要活命就必须套到 302的位置
所以地图数组的范围最小为302,而不是300

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <queue>
using namespace std;
const int MAXSIZE = 400;
const int dx[5]={0,1,0,-1,0};//这里有5个方向,在后面有用的,第一个表示原地
const int dy[5]={0,0,1,0,-1};
int map[MAXSIZE][MAXSIZE];
struct Node{
int x,y,time;
};
int bfs()
{
if(map[0][0]==0)//表示原地被秒杀,直接返回-1
return -1;
else if(map[0][0]==-1)//表示原点安全,直接不用动
return 0;
Node temp,now;//temp表示临时位置,now表示当前位置和时间
temp.x=temp.y=temp.time=0;//初始化为原点
queue<Node> q;
q.push(temp);
while(!q.empty())
{
now = q.front();q.pop();//注意出队列
for(int i=1;i<5;i++)
{
temp.x = now.x+dx[i];
temp.y = now.y+dy[i];
temp.time = now.time + 1;//记得时间 + 1表示走一步
if(temp.x<0 || temp.y<0) //出界直接跳过判断
continue;
if(map[temp.x][temp.y] == -1)//如果我们找到一个安全的位置就返回这个最短的时间
return temp.time;
if(temp.time>=map[temp.x][temp.y])//如果时间上大于或等于了也不能走
continue;
map[temp.x][temp.y] = temp.time;//表示在t时刻该点可以走的
q.push(temp);//如果是可以走的点,就把它放入队列,
}
}
return -1;//没找到
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int x,y,t;
fill(map[0],map[0]+MAXSIZE*MAXSIZE,-1);//初始化为-1
while(n--)
{
scanf("%d%d%d",&x,&y,&t);
for(int i=0;i<5;i++)
{
int tx = x+dx[i],ty = y+dy[i];
if(tx<0 || ty<0)//无效范围
continue;
if(map[tx][ty]==-1)//标记流星掉落时间
map[tx][ty] = t;
else//被重复摧毁的取较小值
map[tx][ty] = min(map[tx][ty],t);
}
}
int themin = bfs();
printf("%d\n",themin);
}
return 0;
}


奈何我冒泡的算法如果打动你超时的心!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: