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适合求最优解 这边再次提醒拓扑变换的思想在图形辨认中的重要作用,需要找寻 ...
随机推荐
- Centos中安装Sublime编辑器
Centos中安装Sublime编辑器 1.从官网下载相应操作系统的下的安装包(http://www.sublimetext.com/2),这里下的是linux下的安装包 2.解压安装包,并将其放在/ ...
- vagrant拷贝后vagrant file需要加的配置
config.ssh.forward_agent config.ssh.username = "vagrant" config.ssh.password = "vagra ...
- iOS中的界面多选功能--(UICollectionView)
文/Jacob_Pan(简书作者)原文链接:http://www.jianshu.com/p/9d28ebd0f5a2著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 最近做项目接触了一 ...
- Android实现左右滑动效果
本示例演示在Android中实现图片左右滑动效果. 关于滑动效果,在Android中用得比较多,本示例实现的滑动效果是使用ViewFlipper来实现的,当然也可以使用其它的View来实现.接下来 ...
- [转]iOS开发使用半透明模糊效果方法整理
转自:http://www.molotang.com/articles/1921.html 虽然iOS很早就支持使用模糊效果对图片等进行处理,但尤其在iOS7以后,半透明模糊效果得到大范围广泛使用.包 ...
- js 闭包和回调
原文:http://www.cnblogs.com/yuyuj/p/4525530.html 之前的工作都是基于老大搭建的框架,仿照他写的例子写的请求,很多东东也都做好了封装,只需要了解下直接调用就好 ...
- (转).net程序员转战android第三篇---登录模块之静态登录
这一篇我将分2个部分记录登录界面,第一部分是静态登录, 这部分将如何从界面布局.控件使用.文件关系.数据验证.登陆实现等5小块记录. 第二部分是动态登录,这块会基于上面的4小块,在数据验证不是静态数据 ...
- 在CentoOS中安装g++ 并连接Oracle数据库
1.安装运行环境 # yum install gcc-c++ 备注:此时会将gcc-c++和libstdc++-devel都安装上. 2.查看g++是否安装成功[root@MyRHEL 桌面]# g+ ...
- Solr中schema.xml的解释
接Solr-4.10.2与Tomcat整合.schema.xml位于D:\solr\data\solr\collection1\conf\中.1.fieldType节点 name: FieldT ...
- C#中子窗体获取父窗体中控件的内容
今天在做一个联系人管理的C#设计时,遇到了这个问题,我需要将父窗体中的textBox中的值传到子窗体并进行数据库查询操作,我用了new 父窗体().textBox.text;来进行值传递,然而并无卵用 ...