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 ...
随机推荐
- android stream media
http://www.cnblogs.com/skyseraph/archive/2012/03/31/2427593.html http://www.cnblogs.com/lingyunhu/p/ ...
- Python实现简单HTTP服务器(一)
一.返回固定内容 # coding:utf-8 import socket from multiprocessing import Process def handle_client(client_s ...
- Shuffle'm Up---poj3087
题目链接 题意:有两个字符串s1,s2:经过交叉问是否得到字符串s,不能输出-1,能就输出交叉的次数 每次重组的串都是s2开始,重新组合时,前面一半是s1,后一半s2: #include<std ...
- python操作docx学习资料
1.博客 (1)写入参考 https://www.cnblogs.com/rencm/p/6285304.html (2)读取参考 http://www.cnblogs.com/zhanghongfe ...
- Mysql中Left Join 与Right Join 与 Inner Join 与 Full Join的区别
看看Left Join 与Right Join 与 Inner Join 与 Full Join对表进行操作后得到的结果. 在数据库中新建两张表,并插入要测试的数据. 新建表: USE [Test] ...
- 十天精通CSS3(2)
圆角效果 border-radius border-radius是向元素添加圆角边框. 使用方法: border-radius:10px; /* 所有角都使用半径为10px的圆角 */ border- ...
-  
这个东西在 html 里是空格占位符,普通的空格在 html 里如果连续的多个可能被认为只有一个,而这个东西你写几个就能占几个空格位.
- LNMPA架构剖析
LAMP或LNMP的劣势: Nginx是小巧而高效的Linux下Web服务器,跟Apache相比,它消耗资源更少,支持的并发连接更多,反向代理功能效率高.静态文件处理更快等等,Nginx可以承受3万以 ...
- L1正则化和L2正则化
L1正则化可以产生稀疏权值矩阵,即产生一个稀疏模型,可以用于特征选择 L2正则化可以防止模型过拟合(overfitting):一定程度上,L1也可以防止过拟合 一.L1正则化 1.L1正则化 需注意, ...
- 7.9 Models -- Connection to An HTTP Server
一.概述 1. 如果你的Ember应用程序需要从一个HTTP服务器加载JSON数据,在你的服务器返回的任何格式中,配置Ember Data的过程将会加载records. 2. store使用一个被称为 ...