POJ3669 Meteor Shower 【BFS】

Meteor Shower

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9163   Accepted: 2594

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

/*
** Problem: POJ No.3669
** Running time: 94MS
** Complier: C++
** Author: Changmu
**
** 题意:给定n个炸弹的爆炸地点和时间,求小明从(0,0)点跑到安全区域的最少时间。
** 题解:直接BFS。
** 坑点:没看数据范围,实际上安全区域可以在给定的范围之外,而且存在一开始就被
** 炸的可能...
*/

#include <stdio.h>
#include <string.h>
#include <queue>

#define maxn 305
#define maxT 1005

int map[maxn][maxn];
const int mov[][2] = {0, 1, 0, -1, -1, 0, 1, 0,};
int N;
struct Node {
    int x, y, t;
};

void Destory(int x, int y, int t) {
    if(map[x][y] == -1 || t < map[x][y])
        map[x][y] = t;
    int xa, ya;
    for(int i = 0; i < 4; ++i) {
        xa = x + mov[i][0];
        ya = y + mov[i][1];
        if(xa < 0 || ya < 0) continue;
        if(-1 == map[xa][ya] || t < map[xa][ya])
            map[xa][ya] = t;
    }
}

void getMap() {
    memset(map, -1, sizeof(map));
    int i, j, x, y, t;
    for(i = 0; i < N; ++i) {
        scanf("%d%d%d", &x, &y, &t);
        Destory(x, y, t);
    }
}

bool check(int x, int y, int t) {
    if(x < 0 || y < 0) return false;
    if(map[x][y] == -1 || map[x][y] > t)
        return true;
    return false;
}

int BFS() {
    if(map[0][0] == -1) return 0;
    if(!map[0][0]) return -1;
    std::queue<Node> Q;
    Node u, v;
    u.x = u.y = u.t = 0;
    Q.push(u);
    while(!Q.empty()) {
        u = Q.front(); Q.pop();
        for(int i = 0; i < 4; ++i) {
            v = u; ++v.t;
            v.x += mov[i][0];
            v.y += mov[i][1];
            if(check(v.x, v.y, v.t)) {
                if(map[v.x][v.y] == -1)
                    return v.t;
                map[v.x][v.y] = 0;
                Q.push(v);
            }
        }
    }
    return -1;
}

int main() {
    // freopen("stdin.txt", "r", stdin);
    while(scanf("%d", &N) == 1) {
        getMap();
        printf("%d\n", BFS());
    }
    return 0;
}
时间: 2024-10-14 06:39:45

POJ3669 Meteor Shower 【BFS】的相关文章

POJ 3669 Meteor Shower【BFS】

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

poj3669 Meteor Shower(BFS)

题目链接:poj3669 Meteor Shower 我只想说这题WA了后去看讨论才发现的坑点,除了要注意原点外,流星范围题目给的是[0,300],到302的位置就绝对安全了... 1 #include<cstdio> 2 #include<cmath> 3 #include<queue> 4 #include<cstring> 5 #include<algorithm> 6 #define CLR(a,b) memset((a),(b),siz

【bfs】【中等难度】tyvj P1234 - bench与奔驰

P1234 - bench与奔驰 From zhangbh001    Normal (OI) 总时限:10s    内存限制:128MB    代码长度 限制:64KB P1234 - bench与奔驰 背景 Background 公园里有个人在练开奔驰 - -!,但是总是撞在bench上 (众人曰:狼来了,快跑啊!) 描述 Description 公园里的bench与奔驰都是无敌的,不会被撞坏.由于开奔驰的人比较"有特点",总是向上下左右四个方向开,而且只会在撞到椅子之后改变方向(

hdoj 1312 Red and Black 【BFS】

题意:一共有四个方向,从'@'出发,找能到达'.'的个数, #是不能通过的. 策略:广搜. 这道题属于最简单的bfs了. 代码: #include<stdio.h> #include<string.h> #include<queue> using std::queue; bool vis[25][25]; char s[25][25]; int n, m; int ans = 0; struct node{ int x, y; }; node st; const int

HDU1242 Rescue 【BFS】

Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 16314    Accepted Submission(s): 5926 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is

HDU 1253 胜利大逃亡 NYOJ 523【BFS】

胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 24608    Accepted Submission(s): 9427 Problem Description Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会. 魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示成A个B*C的

NYOJ 284 坦克大战 【BFS】+【优先队列】

坦克大战 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 Many of us had played the game "Battle city" in our childhood, and some people (like me) even often play it on computer now. What we are discussing is a simple edition of this game. Given a map that co

【BFS】uva10047The Monocycle

/* 本题的特殊之处,到达一个格子时,因为朝向不同,以及接触地面的颜色不同, 会处于不同的状态::::::::: 把(x, y, d, c)作为一个结点,表示所在位置(x, y),方向为d,颜色为c;;;;; ------------------------------------------------------------------------ 在方向上我们把前,左,右编号为0,1,2:::: 颜色,从蓝色开始编号为0,1,2,3:::::::::: ------------------

【BFS】uva11624Fire!

/* bfs宽度遍历 -------------------------------------------------------------------------- 对人和火同时进行bfs,,注意应该先火后人,即如果在人到达该格子前,格子已经着火 则不应该走,最后人走到边界无路可走,则IMPOSSIBLE!!!!!!!!!!!! --------------------------------------------------------------------------- 两次bfs