admin管理员组

文章数量:1530020

题目链接

3669 -- Meteor Shower (poj)

英文题目

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.

中文题目

描述
贝茜听说一场非凡的流星雨即将来临;报道称这些流星将会撞击地球并摧毁它们所击中的一切。由于担心自己的安全,她发誓要找到一个安全的地方(一个永远不会被流星摧毁的地方)。她目前在坐标平面的原点上放牧,想要移动到一个新的,更安全的位置,同时避免被沿途的流星摧毁。

报告说,将有M颗流星(1≤M≤50000)撞击地球,其中流星i将撞击点(Xi, Yi)(0≤Xi≤300;(0≤Ti≤1000)时,0≤Yi≤300。每颗流星都会破坏它所击中的点以及四个直线相邻的点阵点。

贝西在时间0离开原点,可以在第一象限平行于轴线,以每秒一个距离单位的速度移动到任何(通常是4)相邻的直线点,这些点还没有被流星摧毁。她不能在任何时间被定位在一个点大于或等于它被摧毁的时间)。

确定贝西到达安全地点所需的最短时间。

输入
*第一行:单个整数:M

*第2行…M+1:第i+1行包含三个空格分隔的整数:Xi、Yi和Ti

输出
*第一行:贝茜到达安全地点所需的最短时间,如果不可能,则为-1。

思路

bfs找到符合题目要求的位置返回最短时间

具体思路体现代码中的注释

代码

#include<iostream>
#include<string>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
#define N 1001
#define U 0x3f3f3f3f
#define pii pair<int,int>
#define mp make_pair
#define f first
#define s second

int table[301][301];
bool check[301][301];
int dx[5] = { 1,-1,0,0,0 };
int dy[5] = { 0,0,1,-1,0 };

int bfs()


{
    int t = 0;//当前时间
    queue<pii> q;//放置暂时安全的位置
    if (table[0][0] == U)//原点安全返回0
    {
        return t;
    }
    else if (table[0][0] > t)//原点不安全入队列找安全地方
    {
        q.push(mp(0, 0));
        check[0][0] = true;
    }

    //原点为0被砸死while循环不进入返回-1

    while (q.size())
    {
        int sz = q.size();
        for (int j = 0; j < sz; ++j)//找第t秒下1秒暂时安全的左右位置
        {
            pii front = q.front();
            int x = front.f;
            int y = front.s;
            for (int i = 0; i < 4; ++i)//四个方向
            {
                x += dx[i];
                y += dy[i];
                if (x >= 0 && x <= 300 && y >= 0 && y <= 300
                    && check[x][y] == false && t + 1 < table[x][y])//坐标合法且为访问且下一秒位置不会被砸死
                {
                    if (table[x][y] == U)//下一秒永远安全则返回下一秒
                    {
                        return t + 1;
                    }
                        
                    check[x][y] = true;
                    q.push(mp(x, y));
                }
                x -= dx[i];
                y -= dy[i];
            }
            q.pop();
        }
        ++t;
    }
    return -1;//不存在永远安全位置
}

int main()


{
    int n, x, y, t;
    cin >> n;

    memset(table, U, sizeof(table));//每个位置流星掉落最短时间,初始为最大值
    memset(check, false, sizeof(check));//判断是否访问每个位置,false为未访问

    for (int i = 0; i < n; ++i)//读入每个流星雨坐标和时间
    {
        cin >> x >> y >> t;
        for (int j = 0; j < 5; ++j)//流星雨及其相邻坐标
        {
            x += dx[j];
            y += dy[j];
            if (x >= 0 && x <= 300 && y >= 0 && y <= 300 && t < table[x][y])//坐标合法且求最短时间
            {
                table[x][y] = t;
            }
            x -= dx[j];
            y -= dy[j];
        }
    }
    cout << bfs() << endl;
}

本文标签: 流星雨题目代码BFS