BZOJ_1611_[Usaco2008_Feb]_Meteor_Shower流星雨_(bfs)

描述


http://www.lydsy.com/JudgeOnline/problem.php?id=1611

网格图起始位置(0,0),不同时间会有流星落下,导致之后的时间里,该点以及周围四个点都不能走.要走到一定安全的地方,问至少多少时间(一个时间走一步).

 

分析


在网上看到的题解都是记录下每个点最早被毁灭的时间,跑bfs的时候判断这个点被毁灭了没有,然后走到被毁灭时间是INF的点即可.

可是我是模拟的...先把最后一定安全的点都找出来,然后bfs的时候时间每+1,就把那个时间的流行放下去...貌似更快...

 

1.

 

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 const int maxn=300+5,maxt=1000+5,INF=0x7fffffff;
 5 int n;
 6 int t[maxn][maxn];
 7 bool vis[maxn][maxn];
 8 struct nd{ int x,y,step; nd(int x=0,int y=0,int step=0):x(x),y(y),step(step){} };
 9 int go[4][2]={ 0,-1,0,1,-1,0,1,0 };
10 void bfs(){
11     if(t[1][1]==0){
12         printf("-1\n");
13         return;
14     }
15     queue <nd> q;
16     q.push(nd(1,1,0));
17     while(!q.empty()){
18         nd u=q.front(); q.pop();
19         if(t[u.x][u.y]==INF){
20             printf("%d\n",u.step);
21             return;
22         }
23         for(int i=0;i<4;i++){
24             int tx=u.x+go[i][0],ty=u.y+go[i][1];
25             if(!vis[tx][ty]&&tx>=1&&tx<=302&&ty>=1&&ty<=302&&t[tx][ty]>u.step+1){
26                 vis[tx][ty]=true;
27                 q.push(nd(tx,ty,u.step+1));
28             }
29         }
30     }
31     printf("-1\n");
32 }
33 void init(){
34     scanf("%d",&n);
35     for(int i=1;i<=302;i++)
36         for(int j=1;j<=302;j++)
37             t[i][j]=INF;
38     for(int i=1;i<=n;i++){
39         int x,y,z;
40         scanf("%d%d%d",&x,&y,&z); x++; y++;
41         t[x][y]=min(t[x][y],z);
42         for(int i=0;i<4;i++){
43             int tx=x+go[i][0],ty=y+go[i][1];
44             t[tx][ty]=min(t[tx][ty],z);
45         }
46     
47     }
48 }
49 int main(){
50     init();
51     bfs();
52     return 0;
53 }
View Code

 

 

2.

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3  
 4 const int maxn=300+5,maxt=1000+5;
 5 int n;
 6 bool mark1[maxn][maxn];
 7 bool mark2[maxn][maxn];
 8 bool vis[maxn][maxn];
 9 struct node{ int x,y; node(int x=0,int y=0):x(x),y(y){}};
10 struct nd{ int x,y,step; nd(int x=0,int y=0,int step=0):x(x),y(y),step(step){} };
11 vector <node> t[maxt];
12 int go[4][2]={ 0,-1,0,1,-1,0,1,0 };
13 void bfs(){
14     queue <nd> q;
15     q.push(nd(1,1,0));
16     int now=-1;
17     while(!q.empty()){
18         nd u=q.front(); q.pop();
19         if(!mark1[u.x][u.y]){
20             printf("%d\n",u.step);
21             return;
22         }
23         if(u.step!=now){
24             for(int i=0;i<t[u.step].size();i++){
25                 int x=t[u.step][i].x,y=t[u.step][i].y;
26                 mark2[x][y]=mark2[x-1][y]=mark2[x+1][y]=mark2[x][y-1]=mark2[x][y+1]=true;
27             }
28             now=u.step;
29         }
30         if(mark2[u.x][u.y]) continue;
31         for(int i=0;i<4;i++){
32             int tx=u.x+go[i][0],ty=u.y+go[i][1];
33             if(!vis[tx][ty]&&!mark2[tx][ty]&&tx>=1&&tx<=302&&ty>=1&&ty<=302){
34                 vis[tx][ty]=true;
35                 q.push(nd(tx,ty,u.step+1));
36             }
37         }
38     }
39     printf("-1\n");
40 }
41 void init(){
42     scanf("%d",&n);
43     for(int i=1;i<=n;i++){
44         int a,b,c;
45         scanf("%d%d%d",&a,&b,&c); a++; b++;
46         t[c].push_back(node(a,b));
47         mark1[a][b]=mark1[a-1][b]=mark1[a+1][b]=mark1[a][b-1]=mark1[a][b+1]=true;
48     }
49 }
50 int main(){
51     init();
52     bfs();
53     return 0;
54 }
View Code

 

p.s.

1.这题数据是不是有问题啊...我坐标+1,要开到302才能过...

 

1611: [Usaco2008 Feb]Meteor Shower流星雨

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 1284  Solved: 561
[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

 

posted @ 2016-05-27 23:31  晴歌。  阅读(279)  评论(2编辑  收藏  举报