[]poj Meteor Shower

Source: Internet
Author: User
Tags time 0

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 16313 Accepted: 4291

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 (transfer from http://poj.org/problem?id=3669) First of all, to talk about the main idea of the topic, Bessie from the origin, and then there are N meteors will fall at some point, they will destroy the hit of this square will destroy the adjacent blocks, Bessie can not go through the broken block, Ask Bessie to a safe place (will not be hit by any meteor) at least how much time (moving a box of time is 1), if not possible, then output-1 first can preprocess a block by the meteor hit the smallest moment, and then BFS, determine whether to arrive at this time before this block, If it is secure, set it to INF.  The first INF found is an answer. Note that although there is time, but still want to judge the weight, the first time forget, then on the tle. Code:
1 /*2 * poj.org3 * problem#36694 * Accepted5 * Time:266ms6 * memory:620k7  */8#include <iostream>9#include <cstdio>Ten#include <fstream> One#include <cstring> A#include <queue> -#include <algorithm> - #defineINF 0XFFFFFFF the #defineSmin (A, b) a = min (a, b); - using namespacestd; -typedefBOOLBoolean; -Template<typename t> +InlinevoidReadinteger (t&u) { -     Charx; +      while(!isdigit (x =GetChar () ))); A      for(U = x-'0'; IsDigit ((x = GetChar ())); U = (U <<1) + (U <<3) + X-'0'); at ungetc (x, stdin); - } -Template<typename t>classmatrix{ -      Public: -S Mp; -         intlines; in         introws; - Matrix ():p (NULL) {} toMatrix (intLinesintrows): Lines (lines), rows (rows) { +p =Newt[(lines *rows)]; -         } thet*operator[](intPOS) { *             return(P + pos *lines); $         }Panax Notoginseng }; - intN; theMatrix<boolean>visited; +matrix<int>Down ; AInlinevoidinit () { thevisited = matrix<boolean> (303,303); +Down = matrix<int> (303,303); -Fill (DOWN.P, DOWN.P +303*303, INF); $memset (VISITED.P,false,sizeof(Boolean) *303*303); $ Readinteger (n); -      for(inti =1, A, B, C; I <= N; i++){ - Readinteger (a); the Readinteger (b); - Readinteger (c);Wuyi smin (Down[a][b], c); the         ifA1>=0) Smin (Down[a-1][b], c); -Smin (Down[a +1][b], c); Wu         if(B-1>=0) Smin (Down[a][b-1], c); -Smin (Down[a][b +1], c); About     } $ } -typedefclasspoint{ -      Public: -         intx; A         inty; +         intStep; thePoint (Const intx =0,Const inty =0,Const intStep =0): X (x), Y (y), step (step) {} - }point; $ intresult =-1; theQueue<point>que; the Const intmove[2][4] = {{1, -1,0,0}, {0,0,1, -1}}; the voidsolve () { the     if(down[0][0] ==INF) { -result =0; in         return; the     } the Que.push (Point ()); Aboutvisited[0][0] =true; the      while(!Que.empty ()) { thePoint e =Que.front (); the Que.pop (); +          for(inti =0; I <4; i++){ -Point EU = point (e.x + move[0][i], e.y + move[1][i], E.step +1); the             if(eu.x >=0&& eu.y >=0&& Eu.step < DOWN[EU.X][EU.Y] &&!Visited[eu.x][eu.y]) {Bayi                 if(Down[eu.x][eu.y] = =INF) { theresult =Eu.step; the                     return; -                 } -VISITED[EU.X][EU.Y] =true; the Que.push (EU); the             } the         } the     } - } the intMain () { the init (); the solve ();94printf"%d", result); the     return 0; the}

[]poj 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.