[Usaco feb08] meteor shower

Source: Internet
Author: User

138. [usaco feb08] meteor shower

★☆Input file:meteor.inOutput file:meteor.outSimple comparison
Time Limit: 1 s memory limit: 128 MB

Bessie heard a shocking news: a meteor shower is about to attack the entire farm, because the meteor volume is too large, they cannot burn up before hitting the ground, at that time, all things it hits will be destroyed. Naturally, Bessie began to worry about her own security issues. In the name of the smartest cow in the FJ farm, she must arrive at a safe place before being hit by a meteor (that is, a land that will not be hit by any meteor ). If you place the farm in a Cartesian coordinate system, Bessie is now at the origin, and Bessie cannot embark on a land hit by a meteor.

According to the forecast, a total of m meteor (1 <= m <= 50,000) will fall on the farm, 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, Bessie cannot walk on these grids.

Bessie starts to act at moment 0. It can only act in the First quadrant, parallel to the coordinate axis, and she can move to adjacent (usually four) at every moment) 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, Bessie can only appear in the grid at the moment t.

Please calculate the minimum time required for Bessie to reach a safe grid.

Program name: meteor

Input Format:

  • Row 1st: 1 positive integer: m
  • 2nd. m + 1 row: I + 1 Act 3 integers separated by spaces: X_ I, y_ I, and t_ I

Input example (meteor. In ):

40 0 22 1 21 1 20 3 5

Input description:

A total of four meteors will fall to the farm. The coordinates of the landing points are (0, 0), (2, 1), (1, 1), and (0, 3) The time ranges are, and.

T = 0 T = 2 T = 55 | ....... 5 | ....... 5 | ....... 4 | ....... 4 | ....... 4 | #...... * = meteor point 3 | ....... 3 | ....... 3 | *#..... 2 | ....... 2 |. ##.... 2 | ###.... # = walking Restricted Area 1 | ....... 1 | #**#... 1 | ####... 0 | B ...... 0 | *##.... 0 | ###.... -------------- ------------ 0 1 1 2 3 4 5 6 0 1 3 4 5 6 0 1 3 4 5 6

Output Format:

  • Row 3: output an integer, that is, the minimum time spent on the escape of Bessie. If Bessie cannot survive in the meteor shower in any way, output-1

Output example (meteor. out ):

5

Output description:

If we observe the farm at t = 5, we can find that the safe grid closest to Bessie is ()-but because of the early landing of the second meteor, Bessie ran directly) the road line is blocked. The second nearest security grid to Bessie is (), but it is the same. The next grid is on the line. In these grids, (), (), and () can all arrive within five units of time.

5 | ....... 4 | ....... 3 | 3 4 5 .... 2 | 2 ...... bessie's location at each time | 1 ...... 0 | 0 ...... -------------- 0 1 2 3 4 5 6
 
 
Simple search. Note that the data on OJ reaches 301 ..
#include<cstdio>#include<vector>#include<queue>using namespace std;struct node{    int x,y;    int step;}s_pos;int  map[333][333];int  cnt[333][333];bool vis[333][333];int m,flag,ans;int dx[]={1,-1,0,0};int dy[]={0,0,-1,1};bool check(int x,int y){    return x>=0&&x<=301&&y>=0&&y<=301;}void bfs(){    queue<node > q;    s_pos.x=0;s_pos.y=0; s_pos.step=0;    q.push(s_pos);    vis[0][0]=true;    while(!q.empty()){        node now = q.front(); q.pop();        if(map[now.x][now.y]==-1){            flag=1;            ans=now.step;            return;        }        for(int i=0;i<4;i++){            node next=now;            next.x+=dx[i]; next.y+=dy[i];  next.step++;            if(check(next.x,next.y)&&!vis[next.x][next.y]&&               (next.step<map[next.x][next.y]||map[next.x][next.y]==-1)){                vis[next.x][next.y]=true;                q.push(next);            }        }    }}int main(){    freopen("meteor.in","r",stdin);    freopen("meteor.out","w",stdout);    int a,b,t;    scanf("%d",&m);    for(int i=0;i<=301;i++) for(int j=0;j<=301;j++) map[i][j]=-1;    for(int i=0;i<m;i++){       scanf("%d%d%d",&a,&b,&t);       if(map[a][b]==-1||t<map[a][b])       map[a][b]=t;       for(int j=0;j<4;j++){          int nx=a+dx[j]; int ny=b+dy[j];          if(check(nx,ny)&&(map[nx][ny]==-1||t<map[nx][ny])){              map[nx][ny]=t;          }       }    }    bfs();    if(flag)    printf("%d\n",ans);    else    printf("-1\n");    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.