POJ-3669 Meteor shower (BFS)

Source: Internet
Author: User

http://poj.org/problem?id=3669

Note Understanding Test instructions: There is a M planet will fall in the grid (first quadrant), the second planet in TI time will destroy (Xi,yi) this point and the surrounding points, a person started at the origin, and then only in the first quadrant to walk, each unit of time to move one block, can only move to the current not destroyed point, Ask how long it takes to get to a safe place.

Start test instructions understand wrong, do not understand that each planet destroyed up to 5 points, we can preprocess the planet destroyed by the point, and then use BFS search.

1#include <iostream>2#include <cstdio>3#include <cmath>4#include <vector>5#include <cstring>6#include <string>7#include <algorithm>8#include <string>9#include <Set>Ten#include <functional> One#include <numeric> A#include <sstream> -#include <stack> - //#include <map> the#include <queue> -  - #defineCL (arr, Val) memset (arr, Val, sizeof (arr)) -  + #definell Long Long - #defineINF 0x7f7f7f7f + #defineLC L,m,rt<<1 A #defineRC M + 1,r,rt<<1|1 at #definePi ACOs (-1.0) -  - #defineL (x) (x) << 1 - #defineR (x) (x) << 1 | 1 - #defineMID (L, R) (L + R) >> 1 - #defineMin (x, y) (x) < (y)? (x): (y) in #defineMax (x, y) (x) < (y)? (y): (x) - #defineE (x) (1 << (x)) to #defineIabs (x) (x) < 0? -(x): (x) + #defineOut (x) printf ("%i64d\n", X) - #defineLowbit (x) (x) & (-X) the #defineRead () freopen ("A.txt", "R", stdin) * #defineWrite () freopen ("Dout.txt", "w", stdout); $ #defineMAXN 1000000000Panax Notoginseng #defineN 1010 - using namespacestd; the  + intdir[5][2]={-1,0,1,0,0,1,0,-1,0,0}; A intvis[ -][ -]; the structnode + { -     intX,y,time; $ }; $  - intBFS () - { the     if(vis[0][0]==-1)return 0; - node p;WuyiP.x=p.y=p.time=0; theQueue<node>que; - Que.push (p); Wu      while(Que.size ()) -     { AboutNode q=Que.front (); $ Que.pop (); -          for(intI=0;i<4; i++) -         { -p=Q; Ap.x=q.x+dir[i][0]; +p.y=q.y+dir[i][1]; thep.time++; -             if(p.x<0|| p.y<0)Continue;//out of bounds. $             if(vis[p.x][p.y]==-1)returnP.time;//get to the safe place . the             if(P.time<vis[p.x][p.y])//the current point has not yet been destroyed. the             { theVis[p.x][p.y]=p.time;//must be assigned, otherwise it will time out, so that you can prevent the backward situation, saving unnecessary search the Que.push (p); -             } in         } the     } the     return-1; About } the intMain () the { the     //freopen ("A.txt", "R", stdin); +     intm,x,y,t; -scanf"%d",&m); thememset (vis,-1,sizeof(Vis));Bayi      while(m--) the     { thescanf"%d%d%d",&x,&y,&t); -          for(intI=0;i<5; i++) -         { the             intxx=x+dir[i][0]; the             intyy=y+dir[i][1]; the             if(xx>=0&&yy>=0)//pre-processing all the destroyed points and assigning them the minimum time to be destroyed.  the             { -                 if(vis[xx][yy]==-1) vis[xx][yy]=T; the                 Elsevis[xx][yy]=min (t,vis[xx][yy]); the             } the         }94     } the     if(vis[0][0]==0) printf ("-1\n"); the     Elseprintf"%d\n", BFS ()); the    return 0;98}
View Code

POJ-3669 Meteor shower (BFS)

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.