Meteor Shower
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12642   Accepted: 3414

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

Source

 
题意:用最短的时间找到最安全的位置。
注意坑:1.当坐标为(0,0)时,上下左右扩展时下标会越界。
         2. 每个地点的时间可能重复,取最小的。
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
using namespace std; const int INF=0x3f3f3f3f;
const double eps=1e-;
const double PI=acos(-1.0);
#define maxn 310 struct Node
{
int x,y,t;
}; int dx[] = {,,-,};
int dy[] = {,-,,};
int vis[maxn][maxn];
int pic[maxn][maxn];
int n;
void bfs()
{
Node a,b;
a.x = ; a.y = ; a.t = ;
queue<Node> sun;
sun.push(a);
while(!sun.empty())
{
Node temp = sun.front();
sun.pop();
for(int i = ; i < ; i++)
{
b.x = temp.x + dx[i];
b.y = temp.y + dy[i];
b.t = temp.t + ; if(b.t < pic[b.x][b.y] && !vis[b.x][b.y]&&b.x>=&&b.y>=)
{
vis[b.x][b.y] = ;
//puts("QAQ");
if(pic[b.x][b.y] == INF )
{
printf("%d\n", b.t);
return;
}
//printf("%d %d\n", b.x, b.y);
sun.push(b);
}
}
}
printf("-1\n");
}
int main()
{
while(~scanf("%d", &n))
{
memset(pic, INF, sizeof pic);
memset(vis, , sizeof vis);
int x,y,t;
for(int i = ; i < n; i++)
{
scanf("%d%d%d", &x,&y,&t);
pic[x][y] = min(pic[x][y],t);
for(int j = ; j < ; j++)
{
int nx = x + dx[j];
int ny = y + dy[j];
if(nx >= && ny >= )
pic[nx][ny] = min(pic[nx][ny],t);
}
}
vis[][] = ;
bfs();
}
return ;
}

POJ3669(Meteor Shower)(bfs求最短路)的更多相关文章

  1. poj3669 Meteor Shower(BFS)

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

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

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

  3. POJ-3669 Meteor Shower(bfs)

    http://poj.org/problem?id=3669 注意理解题意:有m颗行星将会落在方格中(第一象限),第i颗行星在ti时间会摧毁(xi,yi)这个点和四周相邻的点,一个人开始在原点,然后只 ...

  4. 图-用DFS求连通块- UVa 1103和用BFS求最短路-UVa816。

    这道题目甚长, 代码也是甚长, 但是思路却不是太难.然而有好多代码实现的细节, 确是十分的巧妙. 对代码阅读能力, 代码理解能力, 代码实现能力, 代码实现技巧, DFS方法都大有裨益, 敬请有兴趣者 ...

  5. POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)

    POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...

  6. UVa 816 (BFS求最短路)

    /*816 - Abbott's Revenge ---代码完全参考刘汝佳算法入门经典 ---strchr() 用来查找某字符在字符串中首次出现的位置,其原型为:char * strchr (cons ...

  7. BFS求最短路

    假设有一个n行m列的迷宫,每个单位要么是空地(用1表示)要么是障碍物(用0表示).如和找到从起点到终点的最短路径?利用BFS搜索,逐步计算出每个节点到起点的最短距离,以及最短路径每个节点的前一个节点. ...

  8. UVA 816 -- Abbott's Revenge(BFS求最短路)

     UVA 816 -- Abbott's Revenge(BFS求最短路) 有一个 9 * 9 的交叉点的迷宫. 输入起点, 离开起点时的朝向和终点, 求最短路(多解时任意一个输出即可).进入一个交叉 ...

  9. 6.4.2 用BFS求最短路

    前面的篇幅占了太多,再次新开一章,讲述BFS求最短路的问题 注意此时DFS就没有BFS好用了,因为DFS更适合求全部解,而BFS适合求最优解 这边再次提醒拓扑变换的思想在图形辨认中的重要作用,需要找寻 ...

随机推荐

  1. allVncClients

    VNC Viewer Free Edition 37  RealVNC Ltd.  15,367  Freeware  1021.58 KB VNC is client and server remo ...

  2. git hub 资料汇总

    tobecrazy  Selenium automation test framework    https://github.com/tobecrazy/Demo Smartphone Test F ...

  3. (转)Android创建桌面快捷方式两种方法

    [IT168技术]Android在桌面上生成快捷方式有两种情况,一种是直接在桌面直接生成;一种是长按桌面,在弹出的快捷菜单中生成. 谈谈在桌面上直接生成.个人觉得这个比较爽快,既然都是快捷方式了干嘛还 ...

  4. 浏览器内核Trident/Gecko/WebKit/Presto

    “浏览器内核”主要指渲染引擎(Rendering Engine),负责解析网页语法(如HTML.JavaScript)并渲染.展示网页.因此,所谓的浏览器内核通常也就是指浏览器所采用的渲染引擎, 渲染 ...

  5. Solr 安装与集成IK中文分词器

    创建wangchuanfu core 1.  在example目录下创建wangchuanfu-solr文件夹: 2.  将./solr下的solr.xml拷贝到wangchuanfu-solr目录下 ...

  6. [Editor(typeof(ImageUrlEditor), typeof(UITypeEditor))]无效的可能原因

    开发的用户控件封存在dll中,其他都很顺利,就是这个图片弹出选择路径怎么也搞不出来!(浪费了我半天*2,o(︶︿︶)o 唉,犟脾气拗不过 看了很多搜索信息都说加: [Editor(typeof(Ima ...

  7. SearchFlight_Joker

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. (转)sql server 2008 不允许保存更改,您所做的更改要求删除并重新创建以下表 的解决办法

    启动SQL Server 2008 Management Studio 工具菜单----选项----Designers(设计器)----阻止保存要求重新创建表的更改  取消勾选即可.

  9. FineUI页面布局

    使用布局的优势 相对于为控件设置固定的宽度和高度,布局的重要意义在于子控件可以根据父控件的尺寸自动设置自己的尺寸,在页面尺寸改变时同样有效.如果你在项目中遇到类似如下的需求,就需要考虑布局了: 面板填 ...

  10. C#中的表达式树的浅解

    表达式树可以说是Linq的核心之一,为什么是Linq的核心之一呢?因为表达式树使得c#不再是仅仅能编译成IL,我们可以通过c#生成一个表达式树,将结果作为一个中间格式,在将其转换成目标平台上的本机语言 ...