Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 16022 | Accepted: 4217 |
Descriptionios
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).spa
Determine the minimum time it takes Bessie to get to a safe place.code
Inputblog
* Line 1: A single integer: M
* Lines 2..M+1: Line i+1 contains three space-separated integers: Xi, Yi, and Ti three
Outputip
* Line 1: The minimum time it takes Bessie to get to a safe place or -1 if it is impossible.ci
Sample Inputget
4 0 0 2 2 1 2 1 1 2 0 3 5
Sample Output
5
Source
用一個數組保存每一個位置最先的損毀時刻。將數組每一個元素的值初始化位INF。對於每個流星,更新它下落位置、四個相鄰的位置的損毀時刻:若是這個流星損毀位置的時刻小於這個位置已記錄的損害時刻,就更改該位置的損毀時刻。使用寬度優先搜索,獲得Bessie第一次到達一個安全位置的時間,也就是到達一個損毀時間爲INF的位置所用的時間,若不能到達,則輸出-1。
1 #include <iostream> 2 #include <algorithm> 3 #include <map> 4 #include <vector> 5 #include <functional> 6 #include <string> 7 #include <cstring> 8 #include <queue> 9 #include <set> 10 #include <cmath> 11 #include <cstdio> 12 using namespace std; 13 #define IOS ios_base::sync_with_stdio(false) 14 #define TIE std::cin.tie(0) 15 #define MIN2(a,b) (a<b?a:b) 16 #define MIN3(a,b) (a<b?(a<c?a:c):(b<c?b:c)) 17 #define MAX2(a,b) (a>b?a:b) 18 #define MAX3(a,b,c) (a>b?(a>c?a:c):(b>c?b:c)) 19 typedef long long LL; 20 typedef unsigned long long ULL; 21 const int INF = 0x3f3f3f3f; 22 const double PI = 4.0*atan(1.0); 23 24 const int MAX_L = 305; 25 int n, ans, x, y, t; 26 int board[MAX_L][MAX_L]; 27 bool vis[MAX_L][MAX_L]; 28 int dx[4] = { 1, -1, 0, 0 }, dy[4] = { 0, 0, 1, -1 }; 29 typedef struct Point{ 30 int x, y, t; 31 Point(int x = 0, int y = 0, int t = 0) :x(x), y(y), t(t){} 32 }P; 33 bool solve() 34 { 35 memset(vis, 0, sizeof(vis)); 36 queue<P> que; 37 que.push(P(0, 0)); 38 vis[0][0] = true; 39 while (que.size()){ 40 P p = que.front(); que.pop(); 41 if (board[p.x][p.y]==INF){ 42 ans = p.t; return true; 43 } 44 for (int i = 0; i < 4; i++){ 45 int nx = p.x + dx[i], ny = p.y + dy[i]; 46 if (0 <= nx && nx < MAX_L && 0 <= ny && ny<MAX_L 47 &&!vis[nx][ny] && board[nx][ny]>p.t + 1){ 48 que.push(P(nx, ny, p.t + 1)); 49 vis[nx][ny] = true; 50 } 51 } 52 } 53 return false; 54 } 55 int main() 56 { 57 while (~scanf("%d", &n)){ 58 memset(board, 0x3f, sizeof(board)); 59 for (int i = 0; i < n; i++){ 60 scanf("%d%d%d", &x, &y, &t); 61 board[x][y] = MIN2(board[x][y],t); 62 for (int i = 0; i < 4; i++){ 63 int nx = x + dx[i], ny = y + dy[i]; 64 if (0 <= nx && 0 <= ny){ 65 board[nx][ny] = MIN2(board[nx][ny], t); 66 } 67 } 68 } 69 if (board[0][0] == 0){ puts("-1"); continue;} 70 printf("%d\n", solve() ? ans : -1); 71 } 72 }