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的正半轴以及原点的。
/* ***********************************************
Author :111qqz
Created Time :2016年04月02日 星期六 14时23分42秒
File Name :code/bzoj/1611.cpp
************************************************ */
1#include <cstdio>
2#include <cstring>
3#include <iostream>
4#include <algorithm>
5#include <vector>
6#include <queue>
7#include <set>
8#include <map>
9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
1using namespace std;
2const double eps = 1E-8;
3const int dx4[4]={1,0,0,-1};
4const int dy4[4]={0,-1,1,0};
5const int inf = 0x3f3f3f3f;
6const int N=305;
7int n;
8int star[N][N];
9bool vis[N][N];
10struct node
11{
12 int x,y;
13 int d;
1 bool ok ()
2 {
3 if (x>=0&&y>=0&&!vis[x][y]&&d<star[x][y]) return true;
4 return false;
5 }
1 bool safe()
2 {
3 // cout<<"x:"<<x<<" y:"<<y<<endl;
4 if (star[x][y]==inf) return true;
5 return false;
6 }
1 void where()
2 {
3 cout<<"x:"<<x<<" y:"<<y<<" d :"<<d<<endl;
4 }
5}s;
1void boom(int x,int y,int t)
2{
3 if (x-1>=0) star[x-1][y]=min(star[x-1][y],t);
4 if (y-1>=0) star[x][y-1]=min(star[x][y-1],t);
5 star[x+1][y]=min(star[x+1][y],t);
6 star[x][y+1]=min(star[x][y+1],t);
7 star[x][y] =min(star[x][y] , t);
8}
1int bfs()
2{
3 queue<node>q;
4 q.push(s);
while (!q.empty())
{
1 node pre = q.front();q.pop();
2 if (pre.safe()) return pre.d;
3 // pre.where();
4 for ( int i = 0 ; i < 4 ; i++)
5 {
6 node nxt;
7 nxt.x = pre.x + dx4[i];
8 nxt.y = pre.y + dy4[i];
9 nxt.d = pre.d + 1;
10 // cout<<"nxt.d:"<<nxt.d<<endl;
11 if (nxt.ok())
12 {
13 q.push(nxt);
14 vis[nxt.x][nxt.y] = true;
15 }
16 }
17 }
18 return -1;
19}
20int main()
21{
22#ifndef ONLINE_JUDGE
23 freopen("code/in.txt","r",stdin);
24#endif //感觉题意有问题啊。。。这个第一象限大概是包括坐标轴的。。。不然从原点不可能一步到第一象限啊。
1 scanf("%d",&n);
2 ms(star,0x3f);
3 for ( int i = 1 ;i <= n ; i++)
4 {
5 int x,y,t;
6 scanf("%d %d %d",&x,&y,&t);
7 boom(x,y,t);
1 }
2 s.x = 0 ;
3 s.y = 0;
4 s.d = 0;
5 vis[0][0] = true;
6 printf("%d\n",bfs());
1#ifndef ONLINE_JUDGE
2 fclose(stdin);
3#endif
4 return 0;
5}