poj3669 流星雨
2015-10-23 15:42
411 查看
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
基本的宽搜,先将地图上每一个点被流星击中的时间记录下来,然后宽搜,地图初始化为无穷大;
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
基本的宽搜,先将地图上每一个点被流星击中的时间记录下来,然后宽搜,地图初始化为无穷大;
#include <cstdio> #include <queue> #include <algorithm> #include <cstring> #define MAX_M 50100 using namespace std; const int dx[] = {-1,1,0,0,0}; const int dy[] = {0,0,1,-1,0}; typedef struct{int x,y,t;}P; queue<P> que; int map[500][500],m,last_mer; P mer[MAX_M]; bool vis[500][500]; inline bool inmap(int x,int y){return x >= 0 && y >= 0;} int bfs() { P current;current.x = current.y = current.t = 0; que.push(current); while(!que.empty()) { current = que.front();que.pop(); for(int i = 0;i < 4;i++) { int nx = current.x + dx[i],ny = current.y + dy[i]; int time = current.t + 1; if(inmap(nx,ny) && map[nx][ny] > time && !vis[nx][ny]) { vis[nx][ny] = true; if(map[nx][ny] > last_mer) { return time; } P now;now.x = nx;now.y = ny;now.t = time; que.push(now); } } } return -1; } int main() { scanf("%d",&m); for(int i = 1;i <= m;i++) { scanf("%d%d%d",&mer[i].x,&mer[i].y,&mer[i].t); } memset(map,0x3f,sizeof(map)); for(int i = 1;i <= m;i++) { last_mer = max(last_mer,mer[i].t); for(int j = 0;j < 5;j++) { int nx = mer[i].x + dx[j],ny = mer[i].y + dy[j]; if(inmap(nx,ny) && map[nx][ny] > mer[i].t) { map[nx][ny] = mer[i].t; } } } if(map[0][0] == 0) { printf("-1\n"); } else { printf("%d\n",bfs()); } return 0; }
相关文章推荐
- 初学ACM - 组合数学基础题目PKU 1833
- POJ ACM 1001
- POJ ACM 1002
- POJ 2635 The Embarrassed Cryptographe
- POJ 3292 Semi-prime H-numbers
- POJ 2773 HAPPY 2006
- POJ 3090 Visible Lattice Points
- POJ-2409-Let it Bead&&NYOJ-280-LK的项链
- POJ-1695-Magazine Delivery-dp
- POJ1523 SPF dfs
- POJ-1001 求高精度幂-大数乘法系列
- POJ-1003 Hangover
- POJ-1004 Financial Management
- 用单调栈解决最大连续矩形面积问题
- 2632 Crashing Robots的解决方法
- 1573 Robot Motion (简单题)
- POJ 1200 Crazy Search(简单哈希)
- 【高手回避】poj3268,一道很水的dijkstra算法题
- POJ 1088 滑雪
- poj2387 Til the Cows Come Home—Dijkstra模板