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. CentOS7 安装 OpenCV 的一些问题解决办法

    由于强迫症,实在受不了root权限的旧gcc才能使用boost而普通权限却是最新版gcc,经过一番折腾后,终于把配置全部弄好了,实际上就只需要把新版gcc的各个文件放到系统找到旧gcc的地方,并建立新 ...

  2. 3.java发展简史

    1991年,James Gosling在SUN(Stanford University Network)公司的工程师小组想要设计这样一种小型计算机语言.该语言主要用于像电视盒这样的消费类电子产品.另外 ...

  3. SpringBoot集成Freemarker前端模板

    1.在pom.xml中引入freemarker的jar包 <dependency> <groupId>org.springframework.boot</groupId& ...

  4. 转载--centos7.4安装docker

    参考博文:https://www.cnblogs.com/yufeng218/p/8370670.html 作者:风止雨歇 Docker从1.13版本之后采用时间线的方式作为版本号,分为社区版CE和企 ...

  5. VIM 练级

    https://coolshell.cn/articles/5426.html

  6. nginx 加工上游服务器返回的内容,并返回给客户端

    禁用上游响应头部功能 Syntax: proxy_ignore_headers field ...; Default: — Context: http, server, location 功能介绍:某 ...

  7. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 显示代码:按键提示

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  8. C# Stream篇(—) -- Stream基类-----转载

    C# Stream篇(—) -- Stream基类 写在前头: Stream系列文章共收录7篇,本着备忘和归纳的目的本着备忘和归纳的目的,全部收录于本分类中. 下面是有原文连接,望各位看官还是到原作者 ...

  9. idea 添加 阿里代码规范

    参考: https://blog.csdn.net/weixin_39220472/article/details/80077803

  10. 第2节 Scala中面向对象编程:7、继承的概念以及override和super关键字;8、isInstanceOf 和 asInstanceOf关键字

    6.3.   Scala面向对象编程之继承 6.3.1.     Scala中继承(extends)的概念 Scala 中,让子类继承父类,与 Java 一样,也是使用 extends 关键字: 继承 ...