POJ 3669 Meteor Shower

Source: Internet
Author: User
Tags time 0

Meteor Shower
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 16022 Accepted: 4217

Description

Bessie hears that a extraordinary meteor shower is coming; Reports say that these meteors would crash into Earth and destroy anything they hit. Anxious for her safety, she vows to find she's a safe location (one that's never destroyed by a meteor). She is currently grazing at the origin of the coordinate plane and wants to move to a new, safer location while avoiding B Eing destroyed by meteors along her.

The reports say that M Meteors (1≤ M ≤50,000) would strike, with Meteor I 'll striking point (Xi, Yi) (0≤ Xi ≤ 300; 0≤ Yi ≤300) at time ti (0≤ ti ≤1,000). Each of the meteor destroys the point, 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 Distan Ce unit per second to any of the (often 4) adjacent rectilinear points that is 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

40 0 22 1 21 1 20 3 5

Sample Output

5

Source

Usaco February Silver

Use an array to save the earliest damage time for each location. Initializes the bit INF for the value of each element of the array. For each meteor, update its drop position, four adjacent positions at the time of the damage: if this meteor damage position is less than the time of the damage recorded in this position, change the location of the damage time. Use the width-first search to get the time that the Bessie first arrives at a secure location, that is, the time it takes to reach a location where the time of the INF is destroyed, or 1 if it cannot be reached.

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>Ten#include <cmath> One#include <cstdio> A using namespacestd; - #defineIOS Ios_base::sync_with_stdio (False) - #defineTIE Std::cin.tie (0) the #defineMIN2 (A, B) (A&LT;B?A:B) - #defineMIN3 (A, B) (A<b? ( A&LT;C?A:C):(b<c?b:c)) - #defineMAX2 (A, B) (A&GT;B?A:B) - #defineMAX3 (a,b,c) (a>b? ( A&GT;C?A:C):(b>c?b:c)) +typedefLong LongLL; -typedef unsignedLong LongULL; + Const intINF =0x3f3f3f3f; A Const DoublePI =4.0*atan (1.0); at  - Const intmax_l =305; - intN, ans, x, y, T; - intboard[max_l][max_l]; - BOOLvis[max_l][max_l]; - intdx[4] = {1, -1,0,0}, dy[4] = {0,0,1, -1 }; intypedefstructpoint{ -     intx, y, T; toPoint (intx =0,inty =0,intt =0): X (x), Y (y), T (t) {} + }p; - BOOLSolve () the { *memset (Vis,0,sizeof(Vis)); $Queue<p>que;Panax NotoginsengQue.push (P (0,0)); -vis[0][0] =true; the      while(Que.size ()) { +P p =Que.front (); Que.pop (); A         if(board[p.x][p.y]==INF) { theans = p.t;return true; +         } -          for(inti =0; I <4; i++){ $             intNX = p.x + dx[i], NY = p.y +Dy[i]; $             if(0<= NX && NX < max_l &&0<= NY && ny<max_l -&&!vis[nx][ny] && board[nx][ny]>p.t +1){ -Que.push (P (NX, NY, P.T +1)); theVis[nx][ny] =true; -             }Wuyi         } the     } -     return false; Wu } - intMain () About { $      while(~SCANF ("%d", &N)) { -memset (board,0x3f,sizeof(board)); -          for(inti =0; I < n; i++){ -scanf"%d%d%d", &x, &y, &t); ABoard[x][y] =MIN2 (board[x][y],t); +              for(inti =0; I <4; i++){ the                 intNX = x + dx[i], NY = y +Dy[i]; -                 if(0<= NX &&0<=NY) { $Board[nx][ny] =MIN2 (Board[nx][ny], t); the                 } the             } the         } the         if(board[0][0] ==0) {puts ("-1");Continue;} -printf"%d\n", Solve ()? Ans:-1); in     } the}

POJ 3669 Meteor Shower

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.