Meteor Shower (poj 3669 bfs)


Language:
Default

Meteor Shower

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

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
*/
时间: 2024-11-05 12:24:59

Meteor Shower (poj 3669 bfs)的相关文章

BFS:Meteor Shower(POJ 3669)

     奔跑吧,傻牛 题目大意:这只Bessie的牛又要来闹事了,这次她的任务就是来躲流星雨,流星雨就是在一定时间会从天上砸到Bessie的所在的正方形区域内(Bessie在0,0的位置),然后砸下来后流星雨会把区域内的上下左右区域全部砸烂,并且砸烂后Bessie不能再到这里,Bessie也不能在流星雨打击的地方呆着(也就是只能在流星雨要下来的时间之前才能在这里),问你Bessie要走多少步才能到安全区域 这一道题好玩,我们这样想,Bessie走过的路就是等值的(也就是每走一步都是1),而且还

【POJ 3669 Meteor Shower】简单BFS

流星雨撞击地球(平面直角坐标第一象限),问到达安全地带的最少时间. 对于每颗流星雨i,在ti时刻撞击(xi,yi)点,同时导致(xi,yi)和上下左右相邻的点在ti以后的时刻(包括t)不能再经过(被封锁).安全地带为永远不会被封锁的点. 简单bfs,开始WA在把平面空间上限当成300*300,但根据题目,这只是有流星雨撞击的范围.实际可走的空间理论上没上限,但分析可得,离原点最近的安全地带一定在(302,302)范围内,所以应可把数组至少开为303*303. 后来WA在把G[0][0]==1的情

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

题意 在x,y坐标系,有流星会落下来,给出每颗流星落下来的坐标和时间,问你能否从(0,0)这个点到一个安全的位置.所谓的安全位置就是不会有流星落下的位置. 题解: 广搜,但是这里有一个深坑,就是搜索的时候判断坐标是否越界的时候,题中说0≤x≤300,0≤y≤300.也就是说如果 if(x>300||y>300||x<0||y<0) 就是越界,不用进队列. 注意,就是这个 x>300||y>300 越界条件有问题.不能是300,要比300大,哪怕301也行. 下面看我调试

POJ 3669 Meteor Shower【BFS】

POJ 3669 去看流星雨,不料流星掉下来会砸毁上下左右中五个点.每个流星掉下的位置和时间都不同,求能否活命,如果能活命,最短的逃跑时间是多少? 思路:对流星雨排序,然后将地图的每个点的值设为该点最早被炸毁的时间 #include <iostream> #include <algorithm> #include <queue> #include <cstring> using namespace std; #define INDEX_MAX 512 int

poj 3669 Meteor Shower(bfs)

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 destroy

POJ 3669 Meteor shower 简单BFS, 有坑点

Meteor Shower 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

POJ 3669 Meteor Shower (BFS + 预处理)

Meteor Shower Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9677   Accepted: 2718 Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteors will crash into earth and destroy anything they hi

【POJ - 3669】Meteor Shower(bfs)

-->Meteor Shower 直接上中文了 Descriptions: Bessie听说有场史无前例的流星雨即将来临:有谶言:陨星将落,徒留灰烬.为保生机,她誓将找寻安全之所(永避星坠之地).目前她正在平面坐标系的原点放牧,打算在群星断其生路前转移至安全地点. 此次共有M (1 ≤ M ≤ 50,000)颗流星来袭,流星i将在时间点Ti (0 ≤ Ti  ≤ 1,000) 袭击点 (Xi, Yi) (0 ≤ Xi ≤ 300; 0 ≤ Yi ≤ 300).每颗流星都将摧毁落点及其相邻四点的区

题解报告:poj 3669 Meteor Shower(bfs)

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 destroy