[BZOJ] 1611: [Usaco2008 Feb]Meteor Shower流星雨

1611: [Usaco2008 Feb]Meteor Shower流星雨

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 1654  Solved: 707
[Submit][Status][Discuss]

Description

去年偶们湖南遭受N年不遇到冰冻灾害,现在芙蓉哥哥则听说另一个骇人听闻的消息: 一场流星雨即将袭击整个霸中,由于流星体积过大,它们无法在撞击到地面前燃烧殆尽, 届时将会对它撞到的一切东西造成毁灭性的打击。很自然地,芙蓉哥哥开始担心自己的 安全问题。以霸中至In型男名誉起誓,他一定要在被流星砸到前,到达一个安全的地方 (也就是说,一块不会被任何流星砸到的土地)。如果将霸中放入一个直角坐标系中, 芙蓉哥哥现在的位置是原点,并且,芙蓉哥哥不能踏上一块被流星砸过的土地。根据预 报,一共有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之前的时刻在这个格子里出现。请你计算一下,芙蓉哥哥最少需要多少时间才能到 达一个安全的格子。

Input

* 第1行: 1个正整数:M * 第2..M+1行: 第i+1行为3个用空格隔开的整数:X_i,Y_i,以及T_i

Output

输出1个整数,即芙蓉哥哥逃生所花的最少时间。如果芙蓉哥哥无论如何都无法在流星雨中存活下来,输出-1

Sample Input

4
0 0 2
2 1 2
1 1 2
0 3 5
输入说明:
一共有4颗流星将坠落在霸中,它们落地点的坐标分别是(0, 0),(2, 1),(1, 1)
以及(0, 3),时刻分别为2,2,2,5。

Sample Output

5

HINT

 

样例图示


Source

Silver

 

Analysis

一道简单的BFS

讲个笑话:

虽然题目限定陨石落点坐标是在300内

但是没说人不可以跑到300外

这个坑,真的坑

 

Code

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<queue>
 4 using namespace std;
 5 
 6 const int dir[4][2] = {{0,1},{1,0},{-1,0},{0,-1}},inf = 0x3f3f3f3f;
 7 int map[400][400],n;
 8 
 9 struct node{
10     int x,y,time;
11 };
12 
13 bool Judge(int x,int y){
14     return x < 0 || y < 0;
15 }
16 
17 void Init(){
18     for(int i = 0;i <= 355;i++){
19         for(int j = 0;j <= 355;j++){
20             map[i][j] = inf;
21         }
22     }
23 }
24 
25 void Print(){
26     for(int i = 0;i <= 7;i++){
27         for(int j = 0;j <= 7;j++){
28             if(map[i][j] != inf) printf("%d ",map[i][j]);
29             else cout << 0 << ' ';
30         }cout << endl;
31     }
32     cout << endl;
33 }
34 
35 void bfs(){
36     queue<node> Q;
37     Q.push((node){0,0,0});
38 //    book[0][0] = true;
39     
40     while(!Q.empty()){
41         node now = Q.front();
42         Q.pop();
43         
44         for(int i = 0;i < 4;i++){
45             int nowx = now.x+dir[i][0];
46             int nowy = now.y+dir[i][1];
47             
48             if(Judge(nowx,nowy) || map[nowx][nowy] <= now.time+1) continue;
49             
50             if(map[nowx][nowy] == inf){
51                 printf("%d",now.time+1);
52                 return;
53             }
54             
55             map[nowx][nowy] = now.time+1;
56             Q.push((node){nowx,nowy,now.time+1});
57             
58 //            Print();
59         }
60     }
61     
62     printf("-1");
63 }
64 
65 int main(){
66     scanf("%d",&n);
67     Init();
68     for(int i = 1;i <= n;i++){
69         int a,b,t;
70         scanf("%d%d%d",&a,&b,&t);
71         map[a][b] = min(map[a][b],t);
72         for(int i = 0;i < 4;i++){
73             int nowx = a+dir[i][0];
74             int nowy = b+dir[i][1];
75             if(Judge(nowx,nowy)) continue;
76             map[nowx][nowy] = min(map[nowx][nowy],t);
77         }
78     }
79     
80     if(map[0][0] == inf) printf("0");
81     else if(map[0][0] == 0) printf("-1");
82     else bfs();
83     
84     return 0;
85 } 
亏我还拿我XT哥的代码对拍了好久qwq果然智商太低

 

posted @ 2017-09-02 19:01  Leviaton  阅读(223)  评论(0编辑  收藏  举报