Meteor Shower

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 31358   Accepted: 8064

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

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
题意:Bessie从原点出发,然后有N个流星会在某个时刻落下,它们会破坏砸到的这个方格还会破坏

四边相邻的方块,输出多少时间之后他可以到达安全的地方。如果可能,输出最优解,不可能则输出-1。

#include<iostream>
#include<string.h>
#include<string>
#include<algorithm>
#include<queue>
using namespace std;
int dir[][]={{,},{,-},{,},{,},{-,}};//原点停留(处理爆炸点),上下左右
int a[][];
int n,m,cnt;
struct node
{
int x;
int y;
int t;
}temp,now; int check(int x,int y)
{
if(x>=&&x<&&y>=&&y<)
return ;
else
return ;
} int bfs()
{
if(a[][]==)//刚开始走就被炸死了
return -;
if(a[][]==-)//起点就是安全的地方,不用走了
return ; temp.x=,temp.y=,temp.t=;//起点
queue<node>p;
p.push(temp); while(!p.empty())
{
now=p.front();
p.pop();
for(int i=;i<;i++)//不能原地停留
{
int dx,dy,dt;
dx=now.x+dir[i][];
dy=now.y+dir[i][];
dt=now.t+;
if(check(dx,dy)==)//走出边界
continue;
if(a[dx][dy]==-)//到达安全区域
return dt;
if(dt>=a[dx][dy])//走进爆炸区域
continue;
a[dx][dy]=dt;//更新时间
temp.x=dx;
temp.y=dy;
temp.t=dt;
p.push(temp);//下一个搜索的点进队列
}
}
return -;//到不了安全区域
}
//bfs是从最近的点开始搜索,所以得到的第一个答案就是最近
int main()
{
cin>>n;
memset(a,-,sizeof(a));//初始化地图所有地方都可以走
while(n--)
{
int x,y,t;
cin>>x>>y>>t;
for(int i=;i<;i++)//预处理所有爆炸结束之后的地图
{
int dx,dy;
dx=x+dir[i][];
dy=y+dir[i][];
if(check(dx,dy)==)//超出地图范围
continue;
if(a[dx][dy]==-)
a[dx][dy]=t;
else//if(a[dx][dy]!=-1)
a[dx][dy]=min(a[dx][dy],t);//取最先爆炸的时间
}
}
cout<<bfs()<<endl; return ;
}

POJ 3669 Meteor Shower BFS求最小时间的更多相关文章

  1. POJ 3669 Meteor Shower BFS 水~

    http://poj.org/problem?id=3669 题目大意: 一个人从(0,0)出发,这个地方会落下陨石,当陨石落在(x,y)时,会把(x,y)这个地方和相邻的的四个地方破坏掉,求该人到达 ...

  2. POJ 3669 Meteor Shower(流星雨)

    POJ 3669 Meteor Shower(流星雨) Time Limit: 1000MS    Memory Limit: 65536K Description 题目描述 Bessie hears ...

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

    Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...

  4. POJ 3669 Meteor Shower【BFS】

    POJ 3669 去看流星雨,不料流星掉下来会砸毁上下左右中五个点.每个流星掉下的位置和时间都不同,求能否活命,如果能活命,最短的逃跑时间是多少? 思路:对流星雨排序,然后将地图的每个点的值设为该点最 ...

  5. poj 3669 Meteor Shower(bfs)

    Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...

  6. POJ 3669 Meteor Shower (BFS+预处理)

    Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...

  7. POJ3669(Meteor Shower)(bfs求最短路)

    Meteor Shower Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12642   Accepted: 3414 De ...

  8. poj 3669 Meteor Shower

                                                                                                      Me ...

  9. 【POJ 3669 Meteor Shower】简单BFS

    流星雨撞击地球(平面直角坐标第一象限),问到达安全地带的最少时间. 对于每颗流星雨i,在ti时刻撞击(xi,yi)点,同时导致(xi,yi)和上下左右相邻的点在ti以后的时刻(包括t)不能再经过(被封 ...

随机推荐

  1. RTT之时钟管理

    时钟节拍 :等于 1/T_TICK_PER_SECOND 秒,用 SysTick_Handler实现,在每次加1时都会检查当前线程的时间片是否用完,以及是否有定时器超时.定时值应该为该值的整数倍.非整 ...

  2. 短信通道——阿里大鱼(java)

    综述            注:本文写于2017年6月22日升级之后. 使用阿里大鱼发送短信已经成为一种趋势,因为权威,而且价格也比较适中,被越来越多的公司所采用.在介绍阿里大鱼发送短信之前,首先得拥 ...

  3. vector的使用-Hdu 4841

    圆桌问题 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submis ...

  4. JS实现对对象的深拷贝

    手动遍历对象拷贝 /** * 深拷贝 * @param {*} obj 拷贝对象(object or array) * @param {*} cache 缓存数组 */ function deepCo ...

  5. HHR计划---作业复盘-直播第三课

    一,出租车广告: 1,三个点不合格:周期太长了,大而全互联网产品,不符合MVP原则:业务关键点丢掉了:没有业务认知和成长. 2,关键假设: (1)车主有没有需求呀,画像怎么样? (2)车主收入如何,能 ...

  6. 吴裕雄--天生自然PythonDjangoWeb企业开发:解决使用相对路径名导入包中子模块问题

    问题 将代码组织成包,想用import语句从另一个包名没有硬编码过的包中导入子模块. 解决方案

  7. Python学习笔记008

    while循环 while 条件 : 执行 num =1 while num<=10:    print(num)    num+=1 1-100偶数 方法1 num =2 while num& ...

  8. Java 常用转换

    日期转换 SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = ft.par ...

  9. 【PAT甲级】1036 Boys vs Girls (25 分)

    题意: 输入一个正整数N(题干没指出范围,默认1e5可以AC),接下来输入N行数据,每行包括一名学生的姓名,性别,学号和分数.输出三行,分别为最高分女性学生的姓名和学号,最低分男性学生的姓名和学号,前 ...

  10. 63 滑动窗口的最大值 &&front(),back()操作前一定要判断容器的尺寸不能为0

    给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值.例如,如果输入数组{2,3,4,2,6,2,5,1}及滑动窗口的大小3,那么一共存在6个滑动窗口,他们的最大值分别为{4,4,6,6,6, ...