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

poj 3669 Meteor Shower(bfs)

时间:2015-09-04 23:50:57      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:

Description

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.

 

Input

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

 

Output

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

 

Sample Input

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

 

Sample Output

5

 

Source

 
 
有个小文青去看流星雨,不料流星掉下来会砸毁上下左右中五个点。每个流星掉下的位置和时间都不同,求小文青能否活命,如果能活命,最短的逃跑时间是多少?
有个地方给写错了,wa了很久,太粗心了//code 72行
 
技术分享
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<stack>
 5 #include<vector>
 6 #include<map>
 7 #include<queue>
 8 using namespace std;
 9 #define N 506
10 int mp[N][N];
11 int dirx[]={0,0,-1,1};
12 int diry[]={-1,1,0,0};
13 int vis[N][N];
14 struct Node{
15     int x,y,t;
16 };
17 int bfs(){
18     if(mp[0][0]==-1) return 0;
19     if(mp[0][0]==0) return -1;
20     Node s;
21     s.x=0;
22     s.y=0;
23     s.t=0;
24     vis[0][0]=1;
25     queue<Node>q;
26     q.push(s);
27     Node t1,t2;
28     while(!q.empty()){
29         t1=q.front();
30         q.pop();
31         for(int i=0;i<4;i++){
32             t2.x=t1.x+dirx[i];
33             t2.y=t1.y+diry[i];
34             t2.t=t1.t+1;
35             if(t2.x<0 || t2.x>=N || t2.y<0 || t2.y>=N) continue;
36             if(mp[t2.x][t2.y]==-1){
37                 return t2.t;
38             }
39             if(t2.t>=mp[t2.x][t2.y]) continue;
40             if(vis[t2.x][t2.y]) continue;
41            vis[t2.x][t2.y]=1;
42            //mp[t2.x][t2.y]=t2.t;
43             q.push(t2);
44         }
45     }
46     return -1;
47 }
48 int main()
49 {
50     int m;
51     while(scanf("%d",&m)==1){
52         memset(mp,-1,sizeof(mp));
53         memset(vis,0,sizeof(vis));
54         for(int i=0;i<m;i++){
55             int x,y,t;
56             scanf("%d%d%d",&x,&y,&t);
57             if(mp[x][y]==-1){//处理(x,y)这点
58                 mp[x][y]=t;
59             }
60             else{
61                 mp[x][y]=min(mp[x][y],t);
62             }
63 
64             for(int j=0;j<4;j++){//处理周围4个点
65                 int xx=x+dirx[j];
66                 int yy=y+diry[j];
67                 if(xx<0 || xx>=N || yy<0 || yy>=N) continue;
68                 if(mp[xx][yy]==-1){
69                     mp[xx][yy]=t;
70                 }
71                 else{
72                     mp[xx][yy]=min(mp[xx][yy],t);
73                 }
74             }
75         }
76 
77         printf("%d\n",bfs());
78 
79     }
80     return 0;
81 }
View Code

 

poj 3669 Meteor Shower(bfs)

标签:

原文地址:http://www.cnblogs.com/UniqueColor/p/4782469.html

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