Bzoj1611: [usaco2008 Feb] meteor shower

Source: Internet
Author: User
1611: [usaco Feb] meteor shower time limit: 5 sec memory limit: 64 MB
Submit: 904 solved: 393
[Submit] [Status] Description

Even though Hunan suffered a freeze disaster for N years last year, his brother Furong now heard another shocking news: a meteor shower is about to attack the entire overlord, because the meteor volume is too large, they cannot burn out before they hit the ground, and will have a devastating impact on everything they hit. Naturally, Brother Furong began to worry about his own security issues. In the name of an in-type male, he must arrive at a safe place before being hit by a meteor (that is, a piece of land that won't be hit by any meteor ). If you place the Overlord in a Cartesian coordinate system, the current position of Brother Furong is the origin, and brother Furong cannot embark on a land hit by a meteor. According to the pre-report, a total of m meteor (1 <= m <= 50,000) will fall into the Overlord, the I-th meteor will hit the coordinate (x_ I, y_ I) (0 <= x_ I <= 1,000; 0 <= y_ I <= 300) in the grid. The power of the meteor will turn the grid where it is located and the four adjacent grids around it into a focal point. Of course, brother Furong cannot walk on these grids. Brother Furong starts to act at the moment 0. It can only act in the First quadrant, parallel to the coordinate axis. at every moment, she can move to the adjacent one (usually four) any one in the grid, of course, the target grid should not be burned. If a grid is hit or burned by a meteor at the moment t, Brother Furong can only appear in the grid at the moment t. Please calculate the minimum time required by brother Furong to reach a safe grid.

Input

* Row 1st: 1 positive integer: M * 2nd .. m + 1: I + 1 behavior 3 integers separated by spaces: X_ I, y_ I, and t_ I

Output

The output is an integer, that is, the minimum time it takes for Brother Furong to escape. If brother Furong cannot survive the meteor shower in any way, output-1

Sample input4
0 0 2
2 1 2
1 1 2
0 3 5
Input description:
A total of four meteors will fall into the Overlord. The coordinates of the landing points are (0, 0), (2, 1), (1, 1)
And (0, 3), the time is 2, 2, 5.


T = 0 T = 2 T = 5
5 |... 5 |... 5 |... 5 | .......
4 |... 4 |... 4 |... 4 | #......
* = Meteor point
3 |... 3 |... 3 |... 3 | *#.....
2 | ...... 2 | ###....
# = Walking Restricted Area
1 | ...... 1 | # ** # ...... 1 | ####...
0 | B... 0 | * #... 0 | ###....
------------------------------------------
0 1 2 3 4 5 6 0 1 2 3 5 6 0 1 2 4 5 6


Sample output5
Output description:
If we observe the overlord at t = 5, we can find that the security grid closest to Brother Furong is
() -- But as early as the second meteor landed, Brother Furong ran directly to the () Route and was blocked.
The second safe grid closest to Brother Furong is (), but it is the same. The next lattice is
()-() On this line. In these grids, (), (), and () can all arrive within five units of time.

5 | .......
4 | .......
3 | 3 4 5... in a legal escape plan
2 | 2 ...... brother Furong's location point at every time
1 | 1 ......
0 | 0 ......
--------------
0 1 2 3 4 5 6

Hint Source

Silver

Problem: BFs, you can record the shortest time code for each point to be blown up:
 1 #include<cstdio> 2 #include<cstdlib> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 #include<iostream> 7 #include<vector> 8 #include<map> 9 #include<set>10 #include<queue>11 #define inf 100000000012 #define maxn 300+1013 #define maxm 500+10014 #define ll long long15 using namespace std;16 inline ll read()17 {18     ll x=0,f=1;char ch=getchar();19     while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}20     while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();}21     return x*f;22 }23 struct node24 {25     int x,y,time;26     node(){}27     node(int _x,int _y,int _time){x=_x;y=_y;time=_time;}28 };29 int dx[]={0,1,-1,0};30 int dy[]={1,0,0,-1};31 int v[maxn][maxn],vis[maxn][maxn];32 queue<node>q; 33 void bfs()34 {35     node now=node(0,0,0);36     vis[0][0]=1;37     if(v[0][0]>0)q.push(now);38     int ans=-1;39     while(!q.empty())40     {41         now=q.front();q.pop();42         if(v[now.x][now.y]==inf)43         {44             ans=now.time;45             break;46         }47         for(int i=0;i<4;i++)48         {49             int xx=now.x+dx[i],yy=now.y+dy[i];50             if(xx>=0&&yy>=0&&now.time+1<v[xx][yy]&&!vis[xx][yy])51             {52                 vis[xx][yy]=1;53                 q.push(node(xx,yy,now.time+1));54             }55         }56     }57     printf("%d\n",ans);58 }59 int main()60 {61     freopen("input.txt","r",stdin);62     freopen("output.txt","w",stdout);63     int n=read();64     for(int i=0;i<maxn;i++)65      for(int j=0;j<maxn;j++)66       v[i][j]=inf;67     while(n--)68     {69         int x,y,t,xx,yy;70         x=read();y=read();t=read();71         v[x][y]=min(v[x][y],t);72         for(int i=0;i<4;i++)73         {74             xx=x+dx[i];yy=y+dy[i];75             if(xx>=0&&yy>=0)v[xx][yy]=min(v[xx][yy],t);76         }77     }78     bfs();79     return 0;80 }
View code

 

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.