您的位置:首页 > 其它

POJ3669Meteor Shower【BFS】

2015-08-12 21:23 489 查看
Language:
Default

Meteor Shower

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11570 Accepted: 3163
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

USACO 2008 February Silver
一个人刚开始在原点坐标里第一象限的某些点在某些时间会掉落流星流星会毁灭该点和其相邻的点求人最短多长时间跑到安全地带

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<queue>
using namespace std;
int map[500][500];
int vis[500][500];
int mov[][2]={0,1,0,-1,1,0,-1,0};
struct Node{
int x;
int y;
int dist;
};
int MIN(int a,int b){
return a<b?a:b;
}
bool judge(int x,int y){
if(x>=0&&y>=0)return true;
return false;
}
int bfs(int x,int y){
int i;
memset(vis,0,sizeof(vis));
queue<Node>Q;
Node u,v;
u.x=x;u.y=y;
u.dist=0;
vis[x][y]=1;
Q.push(u);
while(!Q.empty()){
u=Q.front();Q.pop();
if(map[u.x][u.y]==-1)return u.dist;
for(i=0;i<4;++i){
v.x=u.x+mov[i][0];
v.y=u.y+mov[i][1];
v.dist=u.dist+1;
if(judge(v.x,v.y)&&(v.dist<map[v.x][v.y]||map[v.x][v.y]==-1)&&!vis[v.x][v.y]){

vis[v.x][v.y]=1;
Q.push(v);
}
}
}
return -1;
}
int n;
void input(){
int a,b,c;
memset(map,-1,sizeof(map));
for(int i=0;i<n;++i){
scanf("%d%d%d",&a,&b,&c);
if(map[a][b]!=-1)map[a][b]=MIN(map[a][b],c);
else map[a][b]=c;
if(map[a+1][b]!=-1)map[a+1][b]=MIN(map[a+1][b],c);
else map[a+1][b]=c;
if(a-1>=0){
if(map[a-1][b]!=-1)map[a-1][b]=MIN(map[a-1][b],c);
else map[a-1][b]=c;
}
if(map[a][b+1]!=-1)map[a][b+1]=MIN(map[a][b+1],c);
else map[a][b+1]=c;
if(b-1>=0){
if(map[a][b-1]!=-1)map[a][b-1]=MIN(map[a][b-1],c);
else map[a][b-1]=c;
}
}
}
int main()
{
scanf("%d",&n);
input();
printf("%d\n",bfs(0,0));
return 0;
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  POJ3669Meteor Shower