POJ3669(Meteor Shower)(bfs求最短路)
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 (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
Source
#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求最短路)的更多相关文章
- poj3669 Meteor Shower(BFS)
题目链接:poj3669 Meteor Shower 我只想说这题WA了后去看讨论才发现的坑点,除了要注意原点外,流星范围题目给的是[0,300],到302的位置就绝对安全了... #include& ...
- POJ 3669 Meteor Shower BFS求最小时间
Meteor Shower Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 31358 Accepted: 8064 De ...
- POJ-3669 Meteor Shower(bfs)
http://poj.org/problem?id=3669 注意理解题意:有m颗行星将会落在方格中(第一象限),第i颗行星在ti时间会摧毁(xi,yi)这个点和四周相邻的点,一个人开始在原点,然后只 ...
- 图-用DFS求连通块- UVa 1103和用BFS求最短路-UVa816。
这道题目甚长, 代码也是甚长, 但是思路却不是太难.然而有好多代码实现的细节, 确是十分的巧妙. 对代码阅读能力, 代码理解能力, 代码实现能力, 代码实现技巧, DFS方法都大有裨益, 敬请有兴趣者 ...
- POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)
POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...
- UVa 816 (BFS求最短路)
/*816 - Abbott's Revenge ---代码完全参考刘汝佳算法入门经典 ---strchr() 用来查找某字符在字符串中首次出现的位置,其原型为:char * strchr (cons ...
- BFS求最短路
假设有一个n行m列的迷宫,每个单位要么是空地(用1表示)要么是障碍物(用0表示).如和找到从起点到终点的最短路径?利用BFS搜索,逐步计算出每个节点到起点的最短距离,以及最短路径每个节点的前一个节点. ...
- UVA 816 -- Abbott's Revenge(BFS求最短路)
UVA 816 -- Abbott's Revenge(BFS求最短路) 有一个 9 * 9 的交叉点的迷宫. 输入起点, 离开起点时的朝向和终点, 求最短路(多解时任意一个输出即可).进入一个交叉 ...
- 6.4.2 用BFS求最短路
前面的篇幅占了太多,再次新开一章,讲述BFS求最短路的问题 注意此时DFS就没有BFS好用了,因为DFS更适合求全部解,而BFS适合求最优解 这边再次提醒拓扑变换的思想在图形辨认中的重要作用,需要找寻 ...
随机推荐
- 法爱格2014 春夏新款欧美纯色修身高腰无袖吊带V领 拼接性感 连衣裙 黑色 M【图片 价格 品牌 报价】-京东
法爱格2014 春夏新款欧美纯色修身高腰无袖吊带V领 拼接性感 连衣裙 黑色 M[图片 价格 品牌 报价]-京东 法爱格2014 春夏新款欧美纯色修身高腰无袖吊带V领 拼接性感 连衣裙 黑色 M
- [置顶] API相关工作过往的总结之Sandcastle简要使用介绍
Sandcastle介绍 在微软推出Sandcastle之前,人们倾向于选择开源的NDoc(.NET代码文档生成器).NDo可以将 C#.NET 编译生成的程序集和对应的 /doc XML文档,自动转 ...
- poj 1328 Radar Installation(贪心)
Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea i ...
- C/C++基础概念
1.类占用的内存大小: 1)在不同位数的操作系统下,各种数据类型所占用的内存大小:32位和64位操作系统 http://blog.csdn.net/b_zhang/article/details/68 ...
- Linux查看系统状态及备份
1. 如何看当前Linux系统有几颗物理CPU和每颗CPU的核数?cat /proc/cpuinfo将CPU的总核数除以物理CPU的个数,得到每颗CPU的核数.2. 查看系统负载有两个常用的命令,是哪 ...
- python - 类的方法
类的方法分为:普通方法. 静态方法和类方法 调用方式 特征 普通方法 由对象去调用执行,属于类 至少一个self,对象调用 静态方法 属于类,但通过类来调用,不依赖于任何对象,方法内部不需要对象封 ...
- vs2015体验
项目结构 bower.json Bower依据此文件安装需要的前端的包 package.json NPM依据此文件获取对应的包 project.json 包含用于NPM的"poststore ...
- Oracle 获取表结构信息
通过Oracle中的user_tab_cols, user_col_comments, user_constraints, user_cons_columns表联合查询. user_tab_cols用 ...
- REST 相关
REST 相关 REST:Representational State Transfer,表现层状态转化(出现在阮一峰的博客 理解RESTful架构 中,但是,很明显,Representational ...
- hadoop 2.6.0上安装sqoop-1.99.6-bin-hadoop200
第一步:下载sqoop-1.99.6-bin-hadoop200.tar.gz 地址:http://www.eu.apache.org/dist/sqoop/1.99.6/ 第二步:将下载好的sqo ...