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. leetcode 0211

    目录 ✅ 1217. 玩筹码 描述 解答 c java py ✅ 206. 反转链表 描述 解答 c java py ✅ 922. 按奇偶排序数组 II 描述 解答 c 双指针soldier tddo ...

  2. 解决centos7命令无法补全

    背景 偶然发现本地虚拟机centos 7.7配置firewalld-cmd命令行无法补全,手敲命令太多,着实麻烦 解决方案 安装linux命令行补全工具,还能够补全命令参数 yum install b ...

  3. 看完这篇微服务架构设计思想,90%的Java程序员都收藏了

    本博客强烈推荐: Java电子书高清PDF集合免费下载 https://www.cnblogs.com/yuxiang1/p/12099324.html 微服务 软件架构是一个包含各种组织的系统组织, ...

  4. nginx防盗链处理模块referer和secure_link模块

    使用场景:某网站听过URI引用你的页面:当用户在网站点击url时:http头部会通过referer头部,将该网站当前页面的url带上,告诉服务本次请求是由这个页面发起的 思路:通过referer模块, ...

  5. Elasticsearch 如何使用RESTful API

    所有其他语言可以使用 RESTful API 通过端口 9200 和 Elasticsearch 进行通信,你可以用你最喜爱的 web 客户端访问 Elasticsearch .事实上,正如你所看到的 ...

  6. Python学习笔记005

    if if     ==    : xxxx elif     : xxxx else: xxxx 输入字符串 input() 字符串转数值 int() 数值转字符串 str() 输出 print() ...

  7. kafka 高吞吐量的因素

    1.顺序的方式存储数据: 2.批量发送: 3.零拷贝: 来源:咕泡学院

  8. 实现JSP部分内容继承

    我们的网站框架搭好以后,只需要主体部分显示不同的数据. 如果每次代码重写都会造成冗余. 今天欣赏别人代码,学到了 maven 核心代码 <dependency> <groupId&g ...

  9. html-webpack-plugin & clean-webpack-plugin

    html-webpack-plugin Introduction: The HtmlWebpackPlugin simplifies creation of HTML files to serve y ...

  10. Django 学习之cookie与session

    一.cookie和session的介绍 cookie不属于http协议范围,由于http协议无法保持状态,但实际情况,我们却又需要“保持状态”,因此cookie就是在这样一个场景下诞生. cookie ...