POJ 3669 Meteor Shower BFS求最小时间
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求最小时间的更多相关文章
- POJ 3669 Meteor Shower BFS 水~
http://poj.org/problem?id=3669 题目大意: 一个人从(0,0)出发,这个地方会落下陨石,当陨石落在(x,y)时,会把(x,y)这个地方和相邻的的四个地方破坏掉,求该人到达 ...
- POJ 3669 Meteor Shower(流星雨)
POJ 3669 Meteor Shower(流星雨) Time Limit: 1000MS Memory Limit: 65536K Description 题目描述 Bessie hears ...
- 题解报告:poj 3669 Meteor Shower(bfs)
Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...
- POJ 3669 Meteor Shower【BFS】
POJ 3669 去看流星雨,不料流星掉下来会砸毁上下左右中五个点.每个流星掉下的位置和时间都不同,求能否活命,如果能活命,最短的逃跑时间是多少? 思路:对流星雨排序,然后将地图的每个点的值设为该点最 ...
- poj 3669 Meteor Shower(bfs)
Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...
- POJ 3669 Meteor Shower (BFS+预处理)
Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...
- POJ3669(Meteor Shower)(bfs求最短路)
Meteor Shower Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12642 Accepted: 3414 De ...
- poj 3669 Meteor Shower
Me ...
- 【POJ 3669 Meteor Shower】简单BFS
流星雨撞击地球(平面直角坐标第一象限),问到达安全地带的最少时间. 对于每颗流星雨i,在ti时刻撞击(xi,yi)点,同时导致(xi,yi)和上下左右相邻的点在ti以后的时刻(包括t)不能再经过(被封 ...
随机推荐
- LeetCode简单题(四)
题目一: 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你可以尽可能地完成更多的交易(多次买卖一支股票). 注意:你不能同时参与多笔交易(你 ...
- 「AHOI2014/JSOI2014」拼图
「AHOI2014/JSOI2014」拼图 传送门 看到 \(n \times m \le 10^5\) ,考虑根号分治. 对于 \(n < m\) 的情况,我们可以枚举最终矩形的上下边界 \( ...
- flask-script扩展
在项目部署到线上时,指定端口号时,一般都不会在服务器上进行更改,所以使用flask-script就可以在Flask服务器启动时,通过命令行的方式传入参数,而不仅仅通过app.run()方法中传参.具体 ...
- VSCode 出现错误 System.IO.IOException: The configured user limit (128) on the number of inotify instances has been reached.
方案一: sudo vim /etc/sysctl.conf 增加下面内容(环境变量) fs.inotify.max_user_watches = 1638400 fs.inotify.max_use ...
- python 基础文件操作
实时刷新到硬盘里 f= open('hh','w',encoding='utf8') f.write('gyftyftft') f.write('hghgh\njkkjk') f.flush()#实时 ...
- HTTP协议中常用相应的状态码总结
HTTP协议与我们的生活息息相关,尤其对于我们后端开发人员,工作之余我整理了一些HTTP协议响应的一些常见的状态码,希望能帮助大家 HTTP状态码列表 消息(1字头)服务器收到请求,需要请求者继续执行 ...
- 该怎样应对IoT和边缘计算安全挑战
导读 虽然智能家居的响应延迟似乎不是大问题,但如果自动驾驶汽车需要刹车,而数据出现延迟或者被黑客拦截或操纵,这可能造成灾难性后果.这里将需要边缘计算安全. 边缘计算可在靠近远程设备的位置提供计算.存储 ...
- 最近公共祖先(LCA)问题
目录 最近公共祖先 1.向上标记法 2.树上倍增法 3.Tarjan算法 最近公共祖先 定义:给定一颗有根树,若结点 z 既是 x 的祖先,也是 y 的祖先,则称 z 是 x,y 的公共祖先.在 x, ...
- linux中cp指令前面加反斜杠
在cp指令前面加反斜杠可以不弹出是否覆盖的询问而直接覆盖! 如:cp /app/WEB-INF/com/cfg.properties /app_bak/WEB-INF/com/cfg.properti ...
- [经验] Unity3D 里怎么制作天空盒(skybox)
记载一个简单的 天空盒子 的制作方法 第一步: 在 assets 文件夹下新建一个文件夹, 随便取个名字, 不过最好是用来专门管理场景游戏对象的文件夹, 例如放在这个 Skybox 里: ...