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带你学习算法,不管是编程算法,还是深度学习、自然语言处理算法都一网打尽,更有各种计算机新鲜知识和你分享。别急,算法码上来。

全部评论

相关推荐

点赞 评论 收藏
分享
香梨想要offer:人手一个muduo
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务