Meteor Shower POJ-3669 Wide Search

Source: Internet
Author: User

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

4
0 0 2
2 1 2
1 1 2
0 3 5
Sample Output

5


Test instructions

A person needs to find a safe zone from the start point. Given the M coordinate point and the time it will be bombed, the point and the four points around it will also be bombed. The points that have been bombed will never pass.

Exercises

The minimum value of its bombing time is taken from each coordinate point and its surrounding points, and the value of the other points is initialized to-1, because the bombed points cannot go. From the starting point 0, 0 began to apply wide search, calculate the time spent, determine whether the next step is not bombing or has been bombed. Not to be bombed to go. The point that will not be bombed, which is marked as-1, returns the time spent. Note that there are two special cases where the starting point is bombed at 0 o'clock and the starting point is the safe zone.


The code is as follows:

#include <stdio.h> #include <string.h> #include <queue> #include <algorithm> using namespace std
;
int vis[330][330];
int nt[5][2]= {{0,0},{1,0},{0,1},{-1,0},{0,-1}};
struct node {int x,y,t;};
    int BFs () {queue<node>q;
    Node now,tmp;
    if (vis[0][0]==0) return-1;
    else if (vis[0][0]==-1) return 0;
    now.x=0;
    now.y=0;
    now.t=0;
    Q.push (now); while (!
        Q.empty ()) {Now=q.front ();
        Q.pop ();
            for (int i=1; i<5; i++) {tmp=now;
            int tx=tmp.x+nt[i][0];
            int ty=tmp.y+nt[i][1];
            if (tx<0 | | ty<0) continue;
            if (vis[tx][ty]==-1) return (tmp.t+1);
            if ((tmp.t+1) >=vis[tx][ty]) continue;
            vis[tx][ty]=0;
            TMP.X=TX;
            Tmp.y=ty;
            Tmp.t+=1;
        Q.push (TMP);
}} return-1;
    } int main () {int m,k,t,x,y,x1,y1;
    scanf ("%d", &m);
    memset (vis,-1,sizeof (VIS)); For (int j=0; j<m; J + +) {scanf ("%d%d%d", &x,&y,&t);
            for (int i=0; i<=4; i++) {x1=x+nt[i][0];
            Y1=Y+NT[I][1];
            if (x1<0 | | y1<0) continue;
            if (vis[x1][y1]==-1) vis[x1][y1]=t;
        else Vis[x1][y1]=min (vis[x1][y1],t);
    }} printf ("%d\n", BFS ());
return 0;
 }


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.