poj Meteor Shower - 搜索
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 16313 | Accepted: 4291 |
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
/*
* poj.org
* Problem#3669
* Accepted
* Time:266ms
* Memory:620k
*/
#include<iostream>
#include<cstdio>
#include<fstream>
#include<cstring>
#include<queue>
#include<algorithm>
#define INF 0xfffffff
#define smin(a, b) a = min(a, b);
using namespace std;
typedef bool boolean;
template<typename T>
inline void readInteger(T& u){
char x;
while(!isdigit((x = getchar())));
for(u = x - ''; isdigit((x = getchar())); u = (u << ) + (u << ) + x - '');
ungetc(x, stdin);
}
template<typename T>class Matrix{
public:
T *p;
int lines;
int rows;
Matrix():p(NULL){ }
Matrix(int lines, int rows):lines(lines), rows(rows){
p = new T[(lines * rows)];
}
T* operator [](int pos){
return (p + pos * lines);
}
};
int n;
Matrix<boolean> visited;
Matrix<int> down;
inline void init(){
visited = Matrix<boolean>(, );
down = Matrix<int>(, );
fill(down.p, down.p + * , INF);
memset(visited.p, false, sizeof(boolean) * * );
readInteger(n);
for(int i = , a, b, c; i <= n; i++){
readInteger(a);
readInteger(b);
readInteger(c);
smin(down[a][b], c);
if(a - >= ) smin(down[a - ][b], c);
smin(down[a + ][b], c);
if(b - >= ) smin(down[a][b - ], c);
smin(down[a][b + ], c);
}
}
typedef class Point{
public:
int x;
int y;
int step;
Point(const int x = , const int y = , const int step = ):x(x), y(y), step(step){}
}Point;
int result = -;
queue<Point> que;
const int move[][] = {{, -, , }, {, , , -}};
void solve(){
if(down[][] == INF){
result = ;
return;
}
que.push(Point());
visited[][] = true;
while(!que.empty()){
Point e = que.front();
que.pop();
for(int i = ; i < ; i++){
Point eu = Point(e.x + move[][i], e.y + move[][i], e.step + );
if(eu.x >= && eu.y >= && eu.step < down[eu.x][eu.y] && !visited[eu.x][eu.y]){
if(down[eu.x][eu.y] == INF){
result = eu.step;
return;
}
visited[eu.x][eu.y] = true;
que.push(eu);
}
}
}
}
int main(){
init();
solve();
printf("%d", result);
return ;
}
poj Meteor Shower - 搜索的更多相关文章
- poj Meteor Shower
这道题是下流星,流星会下到上下左右中的位置,而且有时间的,要你求出最短到达安全位置的时间. 这道题要注意边界是可以超过300的 #include<stdio.h> #include< ...
- POJ 3669 Meteor Shower(流星雨)
POJ 3669 Meteor Shower(流星雨) Time Limit: 1000MS Memory Limit: 65536K Description 题目描述 Bessie hears ...
- poj 3669 Meteor Shower
Me ...
- POJ 3669 Meteor Shower (BFS+预处理)
Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...
- 题解报告:poj 3669 Meteor Shower(bfs)
Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...
- POJ 3669 Meteor Shower BFS求最小时间
Meteor Shower Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 31358 Accepted: 8064 De ...
- poj 3669 Meteor Shower(bfs)
Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...
- Meteor Shower(POJ 3669)
Meteor Shower Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12816 Accepted: 3451 De ...
- Meteor Shower POJ - 3669 (bfs+优先队列)
Meteor Shower Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26455 Accepted: 6856 De ...
随机推荐
- JS 防止表单重复提交的方法
第一种:用flag标识,下面的代码设置checkSubmitFlg标志: <script language="”JavaScript”"> var checkSubmi ...
- 几种常见web攻击手段及其防御方式
XSS(跨站脚本攻击) CSRF(跨站请求伪造) SQL注入 DDOS web安全系列目录 总结几种常见web攻击手段极其防御方式 总结几种常见的安全算法 XSS 概念 全称是跨站脚本攻击(Cross ...
- 【深入理解javascript】this的用法
引用:this的用法 在函数中this到底取何值,是在函数真正被调用执行的时候确定的,函数定义的时候确定不了 情况1:构造函数 函数作为构造函数用,那么其中的this就代表它即将new出来的对象.另外 ...
- Input的类型(type)
HTML5 新的 Input 类型 HTML5 拥有多个新的表单输入类型.这些新特性提供了更好的输入控制和验证. 本章全面介绍这些新的输入类型: color date datetime datetim ...
- MUTABLE和IMMUTABLE集合
Scala 集合类系统地区分了可变的和不可变的集合.可变集合可以在适当的地方被更新或扩展.这意味着你可以修改,添加,移除一个集合的元素.而不可变集合类,相比之下,永远不会改变.不过,你仍然可以模拟添加 ...
- oracle 之创建用户,表空间,授权,修改用户密码
1.创建表空间 create tablespace ilinkcargoagent logging datafile 'D:\app\Administrator\oradata\ilinkcargoa ...
- iOS 第三方框架-MBProgressHUD
MBProgressHUD提示框官网地址:https://github.com/jdg/MBProgressHUD 官网里已经提供了足够多的例子供我们使用,但在实现开发中,我们用到的只是其中的一小部分 ...
- web api 获取传过来的Json
public async void PostOrder(HttpRequestMessage request) { String responseBodyAsText = await request. ...
- C#中DataTable
.C#中DataTable技术学习 2009-09-10 14:37:18 阅读1496 评论0 字号:大中小 订阅 . 1.在DataTable中执行DataTable.Select(" ...
- json 的相互 转换
using System.Runtime.Serialization.Json; //json 转化为List集合 public List<T> JSONStringToList<T ...