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 (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

(转自http://poj.org/problem?id=3669)

  首先呢讲一下题目的大意,Bessie从原点出发,然后有N个流星会在某个时刻落下,它们会破坏
砸到的这个方格还会破坏四边相邻的方块,Bessie不能经过被破坏的方块,问Bessie到一个安全的地
方(不会被任何流星砸到)至少需要多少时间(移动一格的时间为1),如果不可能,则输出-1
  首先呢可以预处理出某个方块被流星砸中的最小时刻,然后bfs,判断是否在这个时刻之前到达这
个方块,如果是安全的,就设成INF。找到了的第一个INF就是答案。
  注意,虽然有时间,但是还是要判重,第一次忘了,然后就TLE。。
 
Code
 /*
* 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 - 搜索的更多相关文章

  1. poj Meteor Shower

    这道题是下流星,流星会下到上下左右中的位置,而且有时间的,要你求出最短到达安全位置的时间. 这道题要注意边界是可以超过300的 #include<stdio.h> #include< ...

  2. POJ 3669 Meteor Shower(流星雨)

    POJ 3669 Meteor Shower(流星雨) Time Limit: 1000MS    Memory Limit: 65536K Description 题目描述 Bessie hears ...

  3. poj 3669 Meteor Shower

                                                                                                      Me ...

  4. POJ 3669 Meteor Shower (BFS+预处理)

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

  5. 题解报告:poj 3669 Meteor Shower(bfs)

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

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

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

  7. poj 3669 Meteor Shower(bfs)

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

  8. Meteor Shower(POJ 3669)

    Meteor Shower Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12816   Accepted: 3451 De ...

  9. Meteor Shower POJ - 3669 (bfs+优先队列)

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

随机推荐

  1. 静默方式安装10g数据库软件+升级patch+手工建库

    通常我们安装Oracle数据库软件,都是用OUI图形界面来完成的,但有些Unix/Linux系统中并未安装图形系统,也就无法使用图形界面来安装Oracle的产品了,对于这种场景,就只能采用静默方式来安 ...

  2. MySQL在linux上(cmake)的source code安装方法

    1.安装前准备: 1)必备的包和工具  gcc/g++ :MySQL 5.6开始,需要使用g++进行编译.  cmake  :MySQL 5.5开始,使用cmake进行工程管理,cmake需要2.8以 ...

  3. compile time - run-time

    php.net Class member variables are called "properties". You may also see them referred to ...

  4. centos7 安装ftp

    安装VSFTPD 1.首先确认系统内无VSFTPD. rpm -qa|grep vsftpd 若有的话会显示vsftpd-x.x.x.-x.xxx.x86_64 若没有的话会空返回 2.安装VSFTP ...

  5. idea启动java Maven项目,出现" java: 程序包xxxx不存在"

    今天运行Maven项目的时候,出现了,Error:(19, 17) java: 程序包tracetool不存在的情况 本人的解决办法: (1)首先确保maven  pom文件不能报错,即文件上面不能有 ...

  6. CF1053E Euler tour 构造

    正解:构造 解题报告: 传送门! 这种题目一般都是首先考虑合法性 这题也不例外,思考怎么样是合法的呢? 有四点: 1)a[1]=a[2n-1],显然不说 2)若a[i]=a[j],则(j-i)& ...

  7. 洛谷P2279 消防局的设立 [HNOI2003] 贪心

    正解:贪心 解题报告: 传送门! 这题贪心得挺显然的,,,?居然能有蓝,,,是蓝题太水了嘛,,,? 简单说下,这题一看到就能想到,对最低的没被覆盖到的点给它的祖父建一个消防局 没了? 哦这题实现还挺有 ...

  8. Hadoop的那些事儿(转)

    原文:http://www.searchtb.com/tag/mapreduce       在说Hadoop之前,作为一个铁杆粉丝先粉一下Google.Google的伟大之处不仅在于它建立了一个强悍 ...

  9. iOS中textbox文本框清除圆角

    在iOS.Mac safari中,所有的textbox, select, checkbox都会被强制美化为圆角.但在特殊情况下需要清除圆角时发现iOS中使用以下传统的css无效: border-rad ...

  10. sql server内置存储过程、查看系统信息

    1.检索关键字:sql server内置存储过程,sql server查看系统信息 2.查看磁盘空间:EXEC master.dbo.xp_fixeddrives , --查看各个数据库所在磁盘情况S ...