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 Input

4
0 0 2
2 1 2
1 1 2
0 3 5

Sample Output

5
解题思路:一般求最小时间,最少步数,最少交换次数等等---典型的bfs解法。这道题就是求从原点逃离到安全坐标位置需要花费的最小时间,也就是求最少步数,因为每花费1个单位时间可以看作是走1步。流星落下的那个坐标点周围横竖四个方向也会被炸掉,并且要求每到达一个坐标点所花费的时间都要小于该点被炸毁的时间,因此我们可以将图中每个坐标点被炸的最小时间都初始化为INF,读入的时候顺便更新一下该点及周围横竖四个坐标点被流星炸掉的最小时间,然后用一个结构体记录到达每个坐标点所花费的最小时间,再用队列实现按层遍历即可,具体注释在代码里。
AC代码:
 #include<cstdio>
#include<string.h>
#include<queue>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=;
struct node{int x,y,t;}nod;//记录当前到达坐标点的最小时间
int m,xi,yi,ti,mp[maxn][maxn],dir[][]={{-,},{,},{,-},{,}};//方向数组
bool vis[maxn][maxn];//表示该坐标点是否已经被访问
int bfs(int x,int y){//这里不更新原数组mp,mp数组记录的是每个坐标点被流星砸中的最小时间
nod.x=x,nod.y=y,nod.t=;//先初始化第一个坐标点
queue<node> que;
memset(vis,false,sizeof(vis));//全部坐标标记为未访问状态
que.push(nod);//先将第一个节点入队
vis[x][y]=true;//同时将其标记为已访问状态
while(!que.empty()){//按层次遍历
nod=que.front();que.pop();
int tx=nod.x,ty=nod.y,tt=nod.t;
if(mp[tx][ty]==INF)return tt;//表示能到达安全的地方,直接返回已成功逃离到安全坐标位置的最少时间
for(int i=;i<;++i){//遍历横竖四个邻接点
nod.x=tx+dir[i][],nod.y=ty+dir[i][],nod.t=tt+;//走到邻接点的时间只需加1
if(nod.x>=&&nod.y>=&&!vis[nod.x][nod.y]&&nod.t<mp[nod.x][nod.y]){
//注意下界不能小于0,没有上界,因此数组应该大小开比300还大一些
//如果该点还未访问,并且到达该点的时间必须比在该点被破坏的时间还小,这样才可以逃过灾难也符合题目要求
que.push(nod);//将坐标点nod入队
vis[nod.x][nod.y]=true;//并且标记坐标点nod为已访问状态
}
}
}
return -;//表示不能找到安全的位置,则返回-1
}
int main(){
scanf("%d",&m);
memset(mp,0x3f,sizeof(mp));//全部初始化为INF,数组元素用来标记流星砸中该点的最小时间,INF表示该点是安全的
while(m--){
scanf("%d%d%d",&xi,&yi,&ti);
mp[xi][yi]=min(mp[xi][yi],ti);//先更新一下(xi,yi)这一点被流行砸中的最小时间
for(int i=;i<;++i){//同时更新(xi,yi)这一点横竖4个方向同样被流星砸中的最小时间
int nx=xi+dir[i][],ny=yi+dir[i][];
if(nx>=&&ny>=)mp[nx][ny]=min(mp[nx][ny],ti);
//注意下界不能小于0,没有上界
}
}
printf("%d\n",bfs(,));//从坐标原点(0,0)开始进行广度优先搜索
return ;
}

题解报告: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. iOS点击cell时,控件背景色消失的解决方法

    同时调用一下两个方法: - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected ...

  2. School Marks-CodeForces

    B. School Marks time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  3. Ubuntu 16.04下Redis Cluster集群搭建(官方原始方案)

    前提:先安装好Redis,参考:http://www.cnblogs.com/EasonJim/p/7599941.html 说明:Redis Cluster集群模式可以做到动态增加节点和下线节点,使 ...

  4. Redis集群方案之使用豌豆荚Codis搭建(待实践)

    Codis的模式类似Twemproxy,不过这东西引入了ZooKeeper做为Redis的注册与发现来实现高可用. 部署时需要额外增加应用的部署,请根据业务需求来衡量. 部署图类似如下: 当然,上面的 ...

  5. JSP中访问数据库

    在JSP中访问数据库使用的是JSTL标签,本文不按照http://wiki.jikexueyuan.com/project/jsp/database-access.html此方法进行实践,而是采用之前 ...

  6. 详解cisco访问控制列表ACL

    一:访问控制列表概述   ·访问控制列表(ACL)是应用在路由器接口的指令列表.这些指令列表用来告诉路由器哪些数据包可以通过,哪些数据包需要拒绝.   ·工作原理:它读取第三及第四层包头中的信息,如源 ...

  7. Justinmind使用教程(2)——计算表达式及条件用法

    Justinmind的计算表达式以及条件condition的使用对于刚開始学习的人而言比較麻烦. 结合网上了一个教程本文主要针对计算器演示样例进行计算表达式以及条件的使用. 实现目标:依据单位价格(静 ...

  8. 【Android】自己定义View、画家(画布)Canvas与画笔Paint的应用——绘图、涂鸦板app的实现

    利用一个简单的绘图app来说明安卓的图形处理类与自己定义View的应用. 例如以下图,有一个供用户自己随意绘图.涂鸦的app. 这里不做那么花俏了,仅提供黑白两色.但能够改变笔尖的粗细. 实质上这里的 ...

  9. Linux操作系统改动PATH的方法

    1. 暂时改动: 使用export.比如#export PATH=$PATH:/etc/apache/bin 2. 针对用户的改动: vi ~/.bash_profile 增加:export PATH ...

  10. 玩转单元測试之DBUnit

    本文同一时候发表在:http://www.cnblogs.com/wade-xu/p/4547381.html DBunit 是一种扩展于JUnit的数据库驱动測试框架,它使数据库在測试过程之间处于一 ...