BZOJ 1611: [Usaco2008 Feb]Meteor Shower流星雨 (BFS)
1611: [Usaco2008 Feb]Meteor Shower流星雨
Time Limit: 5 Sec Memory Limit: 64 MB Submit: 1239 Solved: 537 [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
样例图示

题意:会有m颗流行,第i颗流行会在t[i]时刻砸到(x[i],y[i])点,砸到的点以及其相邻的四个点都会被烧焦。一个人从(0,0)出发,每次只能走到相邻的四个格子,并且只能走没有被烧焦的格子。问最少要多久才能走到一个安全的地方。
思路:bfs…先用一个数组star[x][y]表示格子(x,y)被烧焦的时间,初始为inf,表示没有被烧焦。
WA了两发。需要注意的是,一个格子可能多次被流星烧焦,烧焦的时间要取最小值。
以及,大概出题人的数学不怎么样?这题中描述的第一象限是包含x,y的正半轴以及原点的。
1/* ***********************************************
2Author :111qqz
3Created Time :2016年04月02日 星期六 14时23分42秒
4File Name :code/bzoj/1611.cpp
5 ************************************************ */
6
7#include <cstdio>
8#include <cstring>
9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33const int N=305;
34int n;
35int star[N][N];
36bool vis[N][N];
37struct node
38{
39 int x,y;
40 int d;
41
42 bool ok ()
43 {
44 if (x>=0&&y>=0&&!vis[x][y]&&d<star[x][y]) return true;
45 return false;
46 }
47
48 bool safe()
49 {
50 // cout<<"x:"<<x<<" y:"<<y<<endl;
51 if (star[x][y]==inf) return true;
52 return false;
53 }
54
55 void where()
56 {
57 cout<<"x:"<<x<<" y:"<<y<<" d :"<<d<<endl;
58 }
59}s;
60
61void boom(int x,int y,int t)
62{
63 if (x-1>=0) star[x-1][y]=min(star[x-1][y],t);
64 if (y-1>=0) star[x][y-1]=min(star[x][y-1],t);
65 star[x+1][y]=min(star[x+1][y],t);
66 star[x][y+1]=min(star[x][y+1],t);
67 star[x][y] =min(star[x][y] , t);
68}
69
70int bfs()
71{
72 queue<node>q;
73 q.push(s);
74
75 while (!q.empty())
76 {
77
78 node pre = q.front();q.pop();
79 if (pre.safe()) return pre.d;
80 // pre.where();
81 for ( int i = 0 ; i < 4 ; i++)
82 {
83 node nxt;
84 nxt.x = pre.x + dx4[i];
85 nxt.y = pre.y + dy4[i];
86 nxt.d = pre.d + 1;
87 // cout<<"nxt.d:"<<nxt.d<<endl;
88 if (nxt.ok())
89 {
90 q.push(nxt);
91 vis[nxt.x][nxt.y] = true;
92 }
93 }
94 }
95 return -1;
96}
97int main()
98{
99#ifndef ONLINE_JUDGE
100 freopen("code/in.txt","r",stdin);
101#endif //感觉题意有问题啊。。。这个第一象限大概是包括坐标轴的。。。不然从原点不可能一步到第一象限啊。
102
103 scanf("%d",&n);
104 ms(star,0x3f);
105 for ( int i = 1 ;i <= n ; i++)
106 {
107 int x,y,t;
108 scanf("%d %d %d",&x,&y,&t);
109 boom(x,y,t);
110
111 }
112 s.x = 0 ;
113 s.y = 0;
114 s.d = 0;
115 vis[0][0] = true;
116 printf("%d\n",bfs());
117
118
119
120
121#ifndef ONLINE_JUDGE
122 fclose(stdin);
123#endif
124 return 0;
125}