码迷,mamicode.com
首页 > 其他好文 > 详细

【流星雨Meteor Shower】

时间:2019-07-17 18:33:41      阅读:102      评论:0      收藏:0      [点我收藏+]

标签:equal   names   奶牛   rect   初始   contains   ssi   har   single   

题意翻译

贝茜听说了一个骇人听闻的消息:一场流星雨即将袭击整个农场,由于流星体积过大,它们无法在撞击到地面前燃烧殆尽,届时将会对它撞到的一切东西造成毁灭性的打击。很自然地,贝茜开始担心自己的安全问题。以FJ牧场中最聪明的奶牛的名誉起誓,她一定要在被流星砸到前,到达一个安全的地方(也就是说,一块不会被任何流星砸到的土地)。如果将牧场放入一个直角坐标系中,贝茜现在的位置是原点,并且,贝茜不能踏上一块被流星砸过的土地。 根据预报,一共有M颗流星(1 <= M <= 50,000)会坠落在农场上,其中第i颗流星会在时刻T_i (0 <= T_i <= 1,000)砸在坐标为(X_i, Y_i) (0 <= X_i <= 300;0 <= Y_i <= 300)的格子里。流星的力量会将它所在的格子,以及周围4个相邻的格子都化为焦土,当然贝茜也无法再在这些格子上行走。

贝茜在时刻0开始行动,它只能在第一象限中,平行于坐标轴行动,每1个时刻中,她能移动到相邻的(一般是4个)格子中的任意一个,当然目标格子要没有被烧焦才行。如果一个格子在时刻t被流星撞击或烧焦,那么贝茜只能在t之前的时刻在这个格子里出现。

请你计算一下,贝茜最少需要多少时间才能到达一个安全的格子。

题目描述

Bessie hears that an extraordinary meteor shower is coming; reports say that these meteors will crash into earth and destroy anything they hit. Anxious for her safety, she vows to find her way to a safe location (one that is never destroyed by a meteor) . She is currently grazing at the origin in the coordinate plane and wants to move to a new, safer location while avoiding being destroyed by meteors along her way.

The reports say that M meteors (1 ≤ M ≤ 50,000) will strike, with meteor i will striking point (Xi, Yi) (0 ≤ Xi ≤ 300; 0 ≤ Yi ≤ 300) at time Ti (0 ≤ Ti  ≤ 1,000). Each meteor destroys the point that 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 distance unit per second to any of the (often 4) adjacent rectilinear points that are 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.

牛去看流星雨,不料流星掉下来会砸毁上下左右中五个点。每个流星掉下的位置和时间都不同,求牛能否活命,如果能活命,最短的逃跑时间是多少?

输入格式

* Line 1: A single integer: M

* Lines 2..M+1: Line i+1 contains three space-separated integers: Xi, Yi, and Ti

输出格式

* Line 1: The minimum time it takes Bessie to get to a safe place or -1 if it is impossible.

输入样例1

4
0 0 2
2 1 2
1 1 2
0 3 5

输出样例1

5

解题思路

  首先,这道题先记录每一块地流星最先砸下的时间,然后搜索时注意一下就好了。但是本题有很多坑。例如虽然给你说流星掉下来的坐标都小于等于300,但是贝茜可以超出这个范围,同样的,流星波也可以超出这个范围,所以这里注意一下。

题解

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int dir[4][2]={0,1,0,-1,1,0,-1,0};
 4 int mp[310][310];//流星 
 5 int flag[310][310];//最少步数 
 6 int m;
 7 struct node{
 8     int x;
 9     int y;
10     int t;//坐标,步数 
11 };
12 queue<node> q;
13 void bfs()
14 {
15     flag[0][0]=0;
16     q.push((node){0,0,0});
17     while(!q.empty())
18     {
19         node head=q.front();
20         q.pop();
21         if(mp[head.x][head.y]>1000)//因为初始化是1001,所以这里代表安全 
22         {
23             cout<<head.t<<endl;//输出步数 
24             return;
25         }
26         for(int i=0;i<4;i++)
27         {
28             int tx=head.x+dir[i][0];
29             int ty=head.y+dir[i][1];
30             if(tx>=0&&ty>=0&&tx<=303&&ty<=303&&!flag[tx][ty]&&head.t+1<mp[tx][ty])
31             {//判断越界并且没被搜过并且流星还没砸下来 
32                 flag[tx][ty]=head.t+1;
33                 q.push((node){tx,ty,head.t+1});
34             }
35         }
36     }
37     cout<<-1<<endl;//不幸,被砸死了 
38 }
39 int main()
40 {
41     scanf("%d",&m);
42     for(int i=0;i<=303;i++)
43     {
44         for(int j=0;j<=303;j++)
45         {
46             mp[i][j]=1001;//初始化 
47         }
48     }
49     for(int i=1;i<=m;i++)
50     {
51         int x,y,t;
52         scanf("%d%d%d",&x,&y,&t);
53         mp[x][y]=min(mp[x][y],t);//最先砸下来的 
54         for(int j=0;j<4;j++)//四个方向 
55         {
56             int tx=x+dir[j][0];
57             int ty=y+dir[j][1];
58             if(tx>=0&&ty>=0)//判断越界 
59                 mp[tx][ty]=min(mp[tx][ty],t);
60         }
61     }
62     bfs();
63     return 0;
64 }

 

【流星雨Meteor Shower】

标签:equal   names   奶牛   rect   初始   contains   ssi   har   single   

原文地址:https://www.cnblogs.com/hualian/p/11202397.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!