poj3669搜索

原题链接(vj)
https://vjudge.net/problem/POJ-3669

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*Sample Input
4
0 0 2
2 1 2
1 1 2
0 3 5
Sample Output
5*/

#include <iostream>
#include <cstring>
#include <queue>
#define inf 0x3f3f3f
using namespace std;
const int maxn=333;
int map[maxn][maxn];
bool vis[maxn][maxn];
struct star
{
int x,y,t;
}aa[50005];
typedef star P;
int dx[5]={1,-1,0,0,0};
int dy[5]={0,0,1,-1,0};
int love;
int bfs(){
memset(vis,0,sizeof(vis));
queue<P>wyh;
P _;
_.x=0;
_.y=0;
_.t=0;
wyh.push(_);
while(wyh.size()){
P cnm=wyh.front();
wyh.pop();
for(int i=0;i<4;i++){
_=cnm;
_.x=_.x+dx[i];
_.y=_.y+dy[i];
++_.t;
if(_.x>=0&&_.y>=0&&map[_.x][_.y]>_.t&&!vis[_.x][_.y]){
vis[_.x][_.y]=true;
if(map[_.x][_.y]>love){
return _.t;
}
wyh.push(_);
}
}
}
return -1;
}
int main(){
ios::sync_with_stdio(false);
int n;
cin>>n;
memset(map,inf,sizeof(map));
for(int i=0;i<n;i++){
cin>>aa[i].x>>aa[i].y>>aa[i].t;
}
for(int i=0;i<n;i++){
love=max(love,aa[i].t);
for(int j=0;j<5;j++){
int nx=aa[i].x+dx[j];
int ny=aa[i].y+dy[j];
if(nx>=0&&ny>=0&&map[nx][ny]>aa[i].t){
map[nx][ny]=aa[i].t;
}
}
}
if(map[0][0]==0){
cout<<"-1"<<endl;
}
else cout<<bfs()<<endl;
return 0;
}

coswindy

-------------本文结束感谢您的阅读-------------