[Usaco2008 Feb] meteor shower [BFS]

Source: Internet
Author: User
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. 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.

BFS Solution

In fact, at the beginning, it was quite difficult to determine whether a certain grid could go through this problem. Then we found that we could pre-process it when we input it;

After BFs, the shortest grid can be found;

Then I made a joke while calculating the time. I don't know how to find the shortest time for BFs.-I went to... I didn't write a diagram topic recently. It was almost dumb.

Then I looked at other people's code and found that I had to record D3 [] more time. When I joined the team, D3 [w] = D3 [H] + 1... in this way, you don't have to worry about how long it takes!

Continue Learning ~ Qaq Mom... It's too weak.

 

Code attached:

#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>using namespace std;const int dx[5]={0,0,-1,1},dy[5]={1,-1,0,0};int n,x,y,t,xx,yy;int f[301][301];int v[301][301],ans=0;int d1[90001],d2[90001],d3[90001];int bfs(int x,int y){int h=1,w=1;d1[h]=x;d2[h]=y;d3[0]=0;v[x][y]=false;xx=x;yy=y;while(h<=w){int temp;for(int i=0;i<4;i++){xx=d1[h]+dx[i];yy=d2[h]+dy[i];if(xx>=0 && xx<=300 && yy>=0 && yy<=300 && v[xx][yy]){if(d3[h]+1<f[xx][yy]){w++;d1[w]=xx;d2[w]=yy;v[xx][yy]=0;                        d3[w]=d3[h]+1;if(f[xx][yy]>1000) return d3[w];}}}h++;}return -1;}int main(){freopen("meteor.in","r",stdin);freopen("meteor.out","w",stdout);//freopen("data.txt","r",stdin);memset(v,true,sizeof(v));memset(f,63,sizeof(f));scanf("%d",&n);f[0][0]=0; for(int i=1;i<=n;i++){scanf("%d%d%d",&x,&y,&t);if(f[x][y]>t)   f[x][y]=t;for(int j=0;j<4;j++)     if(x+dx[j]>=0 && y+dy[j]>=0 && x+dx[j]<=300 && y+dy[j]<=300 && f[x+dx[j]][y+dy[j]]>t)   f[x+dx[j]][y+dy[j]]=t;}    cout<<bfs(0,0);return 0;}

 

[Usaco2008 Feb] 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.