poj3669(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 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

每个格子初始化为最近的一次爆炸时间,安全的格子为INF,BFS,如果到某个格子时间大于等于爆炸时间则返回,否则继续。当到达某个安全格子后退出BFS。

#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <map>
#include <vector>
#include <list>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <limits>
#include <new>
#include <utility>
#include <iterator>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <ctime>
using namespace std;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);
int dx[] = {0, 1, 0, -1, 0}, dy[] = {-1, 0, 1, 0, 0};
const int maxn = 400;

struct Point
{
    int x, y, len;
};

int s[maxn][maxn], vis[maxn][maxn];

bool range(int x, int y)
{
    return x >= 0 && y >= 0;
}

int bfs()
{
    queue<Point> q;
    q.push({0, 0, 0});
    vis[0][0] = 1;
    while (!q.empty())
    {
        Point p = q.front();
        q.pop();
        int x = p.x, y = p.y;
        if (s[x][y] == INF)
            return p.len;
        if (s[x][y] <= p.len)
            continue;
        for (int i = 0; i < 5; ++i)
        {
            int tx = x + dx[i], ty = y + dy[i];
            if (range(tx, ty) && !vis[tx][ty])
            {
                vis[tx][ty] = 1;
                q.push({tx, ty, p.len+1});
            }
        }
    }
    return -1;
}

int main()
{
    for (int i = 0; i < 400; ++i)
        for (int j = 0; j < 400; ++j)
        {
            s[i][j] = INF;
            vis[i][j] = 0;
        }
    int m;
    cin >> m;
    while (m--)
    {
        int x, y, t;
        scanf("%d%d%d", &x, &y, &t);
        for (int i = 0; i < 5; ++i)
            if (range(x+dx[i], y+dy[i]) && s[x+dx[i]][y+dy[i]] > t)
                s[x+dx[i]][y+dy[i]] = t;
    }
    cout << bfs() << endl;
    return 0;
}


算法码上来 文章被收录于专栏

公众号「算法码上来」。godweiyang带你学习算法,不管是编程算法,还是深度学习、自然语言处理算法都一网打尽,更有各种计算机新鲜知识和你分享。别急,算法码上来。

全部评论

相关推荐

07-09 18:33
门头沟学院 Java
这么逆天每年都有人去???&nbsp;填多益网申就是大型的服从性测试
鲁大牛:辅导员在群里发了这个公司我就申了一下。网申居然要写当场开摄像头写两篇不少于三百字的作文。太逆天了
点赞 评论 收藏
分享
哈哈哈哈哈哈哈哈哈哈这个世界太美好了
凉风落木楚山秋:毕业出路老师不管,你盖个章他好交差就完事了,等你盖完毕业了就不关他事情了
点赞 评论 收藏
分享
Twilight_m...:经典我朋友XXXX起手,这是那种经典的不知道目前行情搁那儿胡编乱造瞎指导的中年人,不用理这种**
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务