POJ-3669

Posted Kiven#5197

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ-3669相关的知识,希望对你有一定的参考价值。

Meteor Shower
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 21055   Accepted: 5499

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 (XiYi) (0 ≤ X≤ 300; 0 ≤ Y≤ 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: XiYi, 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

 

题意:

输入m组数,每组分别代表x,y坐标和陨石落在改点时的时间t。从零点出发,问是否有生还的可能。

 

设mp数组初始值为-1,然后存入时间t。

用bfs层次遍历直到找出mp==-1的点,返回现在的t。

 

AC代码:

 1 //#include<bits/stdc++.h>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <iostream>
 5 #include <stack>
 6 #include <queue>
 7 #include <algorithm>
 8 #include <cstring>
 9 #include <cmath>
10 #include <vector>
11 #include <bitset>
12 #include <list>
13 #include <sstream>
14 #include <set>
15 #include <functional>
16 using namespace std;
17 
18 const int MAX=310;
19 
20 struct node{
21     int x;int y;int t;
22 }s,temp;
23 
24 int m;
25 int mp[MAX][MAX];
26 int dir[5][2]={{0,0},{1,0},{0,1},{-1,0},{0,-1}};
27 queue<node> q;
28 
29 int bfs(){
30     if(mp[0][0]==-1) return 0;
31     if(mp[0][0]==0) return -1;
32     s.x=0;
33     s.y=0;
34     s.t=0;
35     q.push(s);
36     while(!q.empty()){
37         temp=q.front();
38         q.pop();
39         for(int i=0;i<5;i++){
40             s.x=temp.x+dir[i][0];
41             s.y=temp.y+dir[i][1];
42             s.t=temp.t+1;
43             if(s.x<0||s.x>=MAX||s.y<0||s.y>=MAX) continue;
44             if(mp[s.x][s.y]==-1) return s.t;
45             if(mp[s.x][s.y]<=s.t) continue;
46             mp[s.x][s.y]=s.t;
47             q.push(s);
48         }
49     }
50     return -1;
51 }
52 
53 int main(){
54     ios::sync_with_stdio(false);
55     cin>>m;
56     memset(mp,-1,sizeof(mp));
57     while(m--){
58         int x,y,t;
59         cin>>x>>y>>t;
60         for(int i=0;i<5;i++){
61             int nx=x+dir[i][0];
62             int ny=y+dir[i][1];
63             if(nx<0||nx>=MAX||ny<0||ny>=MAX)
64             continue;
65             if(mp[nx][ny]==-1)
66             mp[nx][ny]=t;
67             else
68             mp[nx][ny]=min(mp[nx][ny],t);
69         }
70     }
71     cout<<bfs()<<endl;
72     return 0;
73 } 

 

以上是关于POJ-3669的主要内容,如果未能解决你的问题,请参考以下文章

POJ-3669

poj3669 Meteor Shower(bfs预处理)

POJ 3669 Meteor Shower(BFS)

POJ - 3669Meteor Shower(bfs)

poj3669 Meteor Shower (宽度优先搜索)

poj 3669 bfs(这道题隐藏着一个大坑)