您的位置:首页 > 其它

Meteor Shower (poj 3669 bfs)

2015-03-03 16:20 232 查看
Language:
Default

Meteor Shower

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 9929Accepted: 2771
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
题意:M个陨石撞击地球,给出M个陨石的撞击坐标和时刻,每个陨石撞击点的相邻四个点也被摧毁,一旦被摧毁就不能走了,问在(0,0)处的人最少要费多长时间走到安全点。
思路:一个地方可能会被多个陨石摧毁多次,那么就先求出每个点被摧毁的最早时间,然后再bfs。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 305#define MAXN 2005#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b) for(i = a; i <= b; i++)
#define FRL(i,a,b) for(i = a; i < b; i++)
#define mem(t, v) memset ((t) , v, sizeof(t))
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf printf
#define DBG pf("Hi\n")
typedef long long ll;
using namespace std;

struct Meteor
{
int x,y;
int t;
};

int dir[4][2]={1,0,0,1,-1,0,0,-1};
int MeNum;
bool vis[maxn][maxn];
int time[maxn][maxn]; //记录(x,y)处被摧毁的最早时间

bool IsOk(int x,int y)
{
if (x>=0&&x<=maxn&&y>=0&&y<=maxn)
return true;
return false;
}

int bfs()
{
int i;
if (time[0][0]==0) return -1; //如果起点一开始就被摧毁就不可能逃出,返回-1
queue<Meteor>Q;
mem(vis,false);
Meteor st,now;
st.x=0;
st.y=0;
st.t=0;
vis[0][0]=true;
Q.push(st);
while (!Q.empty())
{
st=Q.front();
Q.pop();
if (time[st.x][st.y]==INF)
return st.t;
FRL(i,0,4)
{
now.x=st.x+dir[i][0];
now.y=st.y+dir[i][1];
now.t=st.t+1;
if (IsOk(now.x,now.y)&&!vis[now.x][now.y]&&now.t<time[now.x][now.y]) //走到当前点的时候要没有被访问且该点还没有被摧毁
{
vis[now.x][now.y]=true;
Q.push(now);
}
}
}
return -1;
}

int main()
{
int i,j,x,y,t;
while (~sf(MeNum))
{
mem(time,INF);
FRL(i,0,MeNum)
{
sfff(x,y,t);
if (t<time[x][y]) time[x][y]=t;
FRL(j,0,4)
{
int dx=x+dir[j][0];
int dy=y+dir[j][1];
if (IsOk(dx,dy)&&time[dx][dy]>t)
time[dx][dy]=t;
}
}
pf("%d\n",bfs());
}
return 0;
}
/*
4 0 0 2 2 1 2 1 1 2 0 3 5*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: