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 (XiYi) (0 ≤ X≤ 300; 0 ≤ Y≤ 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: XiYi, and Ti

Output

* Line 1: The minimum time it takes Bessie to get to a safe place or -1 if it is impossible.

Sample

Sample Input

Sample Output

题意:

  巨大流星雨即将袭来。每个流星会对击中的地方以及周围(上下左右四格)造成破坏。Bessie开始时位于(0, 0)位置,并希望逃到一处不会被袭击到的地方。已知每移动一格需要1个时间单位,被流星破坏后的地方不能再进入。给出M个流星在T时刻击中的地方(X, Y),问Bessie能否逃到安全的地方,若能输出最短时间,否则输出-1。

思路:

  首先根据题意自己构建迷宫,将map数组初始化为MAX,表示这个格子被袭击的时间为INF(即永远不会被袭击)。对于每一个流星,将其影响反映到map上,如果破坏范围由重叠,那么格子显示的是较早的破坏时间,下一步用BFS搜索。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#define MAX 0x3f3f3f3f
using namespace std;
int map[][];//存节点信息
int vis[][];//标记数组
int dir[][]= {-,, ,, ,, ,-};//上下左右四个方向
int end;
struct node
{
int x,y;//两点表示节点位置
int time;
} start;//入队列使用
queue<node> q;//队列,自己维护用来存储节点信息
int bfs(int x,int y)
{
memset(vis,,sizeof(vis));
start.x=x,start.y=y,start.time=;//将传递过来的0.0节点放入结构体
vis[x][y]=;//标记为已搜过
q.push(start);//入队列
while(!q.empty())
{
node now=q.front();//取队头元素
q.pop();
if(map[now.x][now.y]==MAX)
{
return now.time;//如果符合条件,返回;根据题意自己写符合的条件。
}
for(int i=; i<; i++)//四个方向入队列
{
start.x=now.x+dir[i][],start.y=now.y+dir[i][];//将第一个方向的入队列
start.time=now.time+;
if(start.x>=&&start.y>=&&vis[start.x][start.y]==&&start.time<map[start.x][start.y])//判断是否越界
{
vis[start.x][start.y]=;
q.push(start);
}
}
}
return -;
}
int main()
{
int n;
while(~scanf("%d",&n))
{
memset(map,MAX,sizeof(map));
for(int j=; j<n; j++)
{
int x,y,time;
scanf("%d%d%d",&x,&y,&time);
if(map[x][y]>time)
map[x][y]=time;
for(int i=; i<; i++)//自己建图过程,一般不需要自己建图
{
int cx,cy;
cx=x+dir[i][],cy=y+dir[i][];
if(cx>=&&cy>=)
if(map[cx][cy]>time)
map[cx][cy]=time;
}
}
int ans=bfs(,);//从00点开始广搜,根据题目要求具体定
cout<<ans<<endl;
} }

POJ 3669 Meteor Shower (BFS+预处理)的更多相关文章

  1. POJ 3669 Meteor Shower BFS求最小时间

    Meteor Shower Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 31358   Accepted: 8064 De ...

  2. POJ 3669 Meteor Shower BFS 水~

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

  3. POJ 3669 Meteor Shower(流星雨)

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

  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. poj 3669 Meteor Shower

                                                                                                      Me ...

  8. 【POJ 3669 Meteor Shower】简单BFS

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

  9. poj3669 Meteor Shower(BFS)

    题目链接:poj3669 Meteor Shower 我只想说这题WA了后去看讨论才发现的坑点,除了要注意原点外,流星范围题目给的是[0,300],到302的位置就绝对安全了... #include& ...

随机推荐

  1. PHP获取远程文件的几种方式

    1.fopen() 2.file_get_contents() 3.fsocket() 4.curl()

  2. ReactiveCocoa源码解析(二) Bag容器的代码实现

    今天博客我接着上篇博客的内容来,上篇博客我们详细的看了ReactiveSwift中的Observer已经Event的代码实现.接下来我们来看一下ReactiveSwift中的结构体Bag的实现.Bag ...

  3. JAVA 23种开发模式详解(代码举例)

    设计模式(Design Patterns) ——可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了 ...

  4. Linux编程之select

    select系统调用的的用途是:在一段指定的时间内,监听用户感兴趣的文件描述符上可读.可写和异常等事件. select 机制的优势 为什么会出现select模型? 先看一下下面的这句代码: int i ...

  5. eclipse中Build Path 导入的包和复制到 lib 包的区别

    Java Build Path是我们编译需要的包,在比如在import ***.***.***时如果没用Java Build Path导入包的话类里面就有红叉,说不识别这个类,build path只是 ...

  6. django-xadmin隐藏菜单不显示

    环境:https://github.com/y2kconnect/xadmin-for-python3.git python3.5 django1.9.12 在adminx.py中的Admin类增加属 ...

  7. 异常:java.lang.NoSuchMethodError: org.apache.poi.ss.usermodel.Workbook.getCellStyleAt

    背景 最近公司所有新项目要使用最新高效快速开发框架nature-framework,框架本身结合NatureMap已经集成excel的高效导入功能,我们要实现高性能的导出功能,因为最新的jxls-2. ...

  8. TSC打印机使用教程终极版

    最近公司做一个资产采集的项目,之前做过此类项目,不过没有整理资料,借这次机会写一下,做个记录. 本教程使用的打印机型号:TSC TTP-244 Plus     官方文档 一.TSC打印机安装 1.机 ...

  9. 从源码的角度看Activity是如何启动的

    欢迎访问我的个人博客,原文链接:http://wensibo.top/2017/07/03/Binder/ ,未经允许不得转载! 大家好,今天想与大家一起分享的是Activity.我们平时接触的最多的 ...

  10. MySQL(三)--函数与谓词

    前文已有涉及,这里作为总结重新整理一下. 一.函数 1.算术函数 NUMERIC 是大多数 DBMS 都支持的一种数据类型,通过 NUMBERIC ( 全体位数, 小数位数 ) 的形式来指定数值的大小 ...