Meteor Shower
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 26455   Accepted: 6856

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

题意:流星来袭,在一个方格中,贝茜在(0,0)点,为了防止被流星击中,贝茜需要移动到安全的位置。一共有n颗流星,给出流星落下的坐标(xi,yi)和时间ti,每一颗流星落地时上下左右的格子也为遭受毁灭。贝茜经过的路程是不能被毁灭的。

题解:首先要建图,用map[][]数组储存好每个点遭受流星打击的时间,不受流星打击的用-1涵盖。(不能用0覆盖,部分点可能在0时刻遭受打击) 然后遍历整张图,找到不受流星打击的点即可,因为要求最短时间,所有要用到优先队列,优先时间最小的点出队列。根据题意我们知道在图内的不受流星打击的点和没有经过流星打击的点能进入队列。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
const int maxn=; int dx[]={,,-,},dy[]={,,,-};
int maze[maxn][maxn];
int vis[maxn][maxn]; struct node
{
int x,y,step;
}temp,a;
bool operator < (const node &a,const node &b)
{
return a.step>b.step;
} int min(int a,int b)
{
return a>b?b:a;
}
void bfs()
{
int i;
memset(vis,,sizeof(vis));
priority_queue<node>q;
a.x=; a.y=;
a.step=;
q.push(a);
vis[][]=;
while(!q.empty())
{
a=q.top();
q.pop();
if(maze[a.x][a.y]==-)
{
cout<<a.step<<endl;
return ;
}
for(i=;i<;i++)
{
temp.x=a.x+dx[i];
temp.y=a.y+dy[i];
temp.step=a.step+;
if(temp.x>= && temp.y>= && (maze[temp.x][temp.y]==- || maze[temp.x][temp.y]>temp.step))
{
if(!vis[temp.x][temp.y])
q.push(temp);
vis[temp.x][temp.y]=;
}
} }
cout<<-<<endl; } int main()
{
int n,x,y,t;
memset(maze,-,sizeof(maze));
cin>>n;
for(int i=;i<n;i++)
{
cin>>x>>y>>t;
if(maze[x][y]==-)
maze[x][y]=t;
else
maze[x][y]=min(maze[x][y],t);
for(int j=;j<;j++)
{
int nx=x+dx[j];
int ny=y+dy[j];
if(nx>= && ny>=)
{
if(maze[nx][ny]==-)
maze[nx][ny]=t;
else
maze[nx][ny]=min(t,maze[nx][ny]);
}
} }
bfs();
return ;
}

Meteor Shower POJ - 3669 (bfs+优先队列)的更多相关文章

  1. BFS:Meteor Shower(POJ 3669)

         奔跑吧,傻牛 题目大意:这只Bessie的牛又要来闹事了,这次她的任务就是来躲流星雨,流星雨就是在一定时间会从天上砸到Bessie的所在的正方形区域内(Bessie在0,0的位置),然后砸下 ...

  2. 【POJ 3669 Meteor Shower】简单BFS

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

  3. poj 3635(bfs+优先队列)

    题目链接:http://poj.org/problem?id=3635 思路:本题主要运用的还是贪心思想,由于要求st->ed的最小花费,那么每经过一个城市,能不加油就尽量不加油,用dp[i][ ...

  4. poj3669 Meteor Shower(预处理+bfs)

    https://vjudge.net/problem/POJ-3669 先给地图a[][]预处理每个位置被砸的最小时间.然后再bfs. 纯bfs,还被cin卡了下时间.. #include<io ...

  5. 【BZOJ】1611: [Usaco2008 Feb]Meteor Shower流星雨(bfs)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1611 一眼题,bfs. #include <cstdio> #include <c ...

  6. bzoj 1611: [Usaco2008 Feb]Meteor Shower流星雨【BFS】

    t记录每个格子最早被砸的时间,bfs(x,y,t)表示当前状态为(x,y)格子,时间为t.因为bfs,所以先搜到的t一定小于后搜到的,所以一个格子搜一次就行 #include<iostream& ...

  7. poj 3669 bfs(这道题隐藏着一个大坑)

    题意 在x,y坐标系,有流星会落下来,给出每颗流星落下来的坐标和时间,问你能否从(0,0)这个点到一个安全的位置.所谓的安全位置就是不会有流星落下的位置. 题解: 广搜,但是这里有一个深坑,就是搜索的 ...

  8. POJ 3669 Meteor Shower【BFS】

    POJ 3669 去看流星雨,不料流星掉下来会砸毁上下左右中五个点.每个流星掉下的位置和时间都不同,求能否活命,如果能活命,最短的逃跑时间是多少? 思路:对流星雨排序,然后将地图的每个点的值设为该点最 ...

  9. poj 3669 Meteor Shower(bfs)

    Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...

随机推荐

  1. NET Core写了一个轻量级的Interception框架[开源]

    NET Core写了一个轻量级的Interception框架[开源] ASP.NET Core具有一个以ServiceCollection和ServiceProvider为核心的依赖注入框架,虽然这只 ...

  2. JS——三种嵌入页面的方式

    一 行间事件 二 页面script标签嵌入 三 外部引入 <!DOCTYPE html> <html lang="en"> <head> < ...

  3. @Slf4j注解的使用

    项目中使用Slf4j日志: private static final Logger log=LoggerFactory.getLogger(TestMain.class); 使用@Slf4j以后,默认 ...

  4. Json规范

    标准格式 书写使用首字母小写驼峰式 {" status":0   //状态 大于0代表正常.小于等于0代表异常 "message":"",/ ...

  5. SSRS-lookupSet-DataSet-分组查询

    SSRS-lookupSet-DataSet-分组查询 来源:http://www.cnblogs.com/biwork/p/3621885.html 目录:http://www.cnblogs.co ...

  6. sql、linq和lambda查询语句比较inner join和group by组合使用及匿名类型的处理

    使用EF自己做的小功能需要遇到inner join和group by组合使用及匿名类型的处理,搜了很多,基本不能满足自己的需要,所以总结了也实现了就自己写出来,已备查看及伙伴查询参考(一般的语句查询就 ...

  7. vfp使用笔记

    1:update数据,根据记录中某个字段的值,从另一个表中查询并填充数据 UPDATE cs2013yy SET cs2013yy.ksh=NVL((SELECT cs2013gkbm.ksh FRO ...

  8. 织梦DeDeCMS友情链接文字显示不全

    文件:/include/taglib/flink.lib.php 把下面代码中的24改为合适的值 $attlist=”type|textall,row|24,titlelen|24,linktype| ...

  9. SAP成都研究院DevOps那些事

    今天的文章来自我的同事平静静,SAP成都研究院一位程序媛.平静静2010年加入SAP,熟悉她的人一般都叫她平静.在她待过的每个小组,平静静都不是最引人瞩目的开发人员,然而她总是能一如既往,保质保量地完 ...

  10. Meaningful Mean

    You are given an integer sequence of length N, a= {a1,a2,…,aN}, and an integer K.a has N(N+1)⁄2 non- ...