zoukankan      html  css  js  c++  java
  • 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;
    }


  • 相关阅读:
    linux下链接静态链接库
    linux消息队列操作(转)
    android 模块编译,mm 命令
    关于函数里对指针赋值的问题
    Android内核和驱动篇Android内核介绍 (转)
    pthread属性使用(转)
    MOD_INC_USE_COUNT和MOD_DEC_USE_COUNT(转)
    linux下配置文件的读写
    从Linux程序中执行shell(程序、脚本)并获得输出结果(转)
    封装错误信息打印的函数
  • 原文地址:https://www.cnblogs.com/godweiyang/p/12203989.html
Copyright © 2011-2022 走看看