POJ3669 Meteor ShowerBFS

Posted 海岛Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ3669 Meteor ShowerBFS相关的知识,希望对你有一定的参考价值。

Meteor Shower
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 37006 Accepted: 9463

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

问题链接POJ3669 Meteor Shower
问题简述:(略)
问题分析:搜索问题,用BFS来解决。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* POJ3669 Meteor Shower */

#include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>

using namespace std;

int dx[] = {0, 0, 0, 1, -1};
int dy[] = {0, 1, -1, 0, 0};
const int DL = sizeof(dx) / sizeof(int);

const int N = 1000;
int g[N + 1][N + 1];
struct Node {
    int x, y, time;
};

int bfs()
{
    if (g[0][0] == 0) return -1;
    else if (g[0][0] == -1) return 0;

    Node t, now;
    t.x = t.y = t.time = 0;
    queue<Node> q;
    q.push(t);
    while (!q.empty()) {
        now = q.front();
        q.pop();
        for (int i = 0; i < DL; i++) {
            t.x = now.x + dx[i];
            t.y = now.y + dy[i];
            t.time = now.time + 1;
            if (0 <= t.x && t.x <= N && 0 <= t.y && t.y <= N) {
                if (g[t.x][t.y] == -1) return t.time;
                if (t.time < g[t.x][t.y]) {
                    g[t.x][t.y] = t.time;
                    q.push(t);
                }
            }
        }
    }
    return -1;
}

int main()
{
    int m, x, y, t;
    while (scanf("%d", &m) != EOF) {
        memset(g, -1, sizeof g);

        for (int i = 1; i <= m; i++) {
            scanf("%d%d%d", &x, &y, &t);

            for (int j = 0; j < DL; j++) {
                int nx = x + dx[j];
                int ny = y + dy[j];
                if (0 <= nx && nx <= N && 0 <= ny && ny <= N)
                    g[nx][ny] = g[nx][ny] == -1 ? t : min(g[nx][ny], t);
            }
        }

        printf("%d\\n", bfs());
    }

    return 0;
}

以上是关于POJ3669 Meteor ShowerBFS的主要内容,如果未能解决你的问题,请参考以下文章

poj3669 Meteor Shower(bfs预处理)

POJ - 3669Meteor Shower(bfs)

poj3669 Meteor Shower (宽度优先搜索)

POJ3669解题报告(bfs)

POJ-3669

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