zoukankan      html  css  js  c++  java
  • POJ 3669 Meteor Shower

    Meteor Shower
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 16022   Accepted: 4217

    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

        用一个数组保存每个位置最早的损毁时刻。将数组每个元素的值初始化位INF。对于每一个流星,更新它下落位置、四个相邻的位置的损毁时刻:如果这个流星损毁位置的时刻小于这个位置已记录的损害时刻,就更改该位置的损毁时刻。使用宽度优先搜索,得到Bessie第一次到达一个安全位置的时间,也就是到达一个损毁时间为INF的位置所用的时间,若不能到达,则输出-1。

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <map>
     4 #include <vector>
     5 #include <functional>
     6 #include <string>
     7 #include <cstring>
     8 #include <queue>
     9 #include <set>
    10 #include <cmath>
    11 #include <cstdio>
    12 using namespace std;
    13 #define IOS ios_base::sync_with_stdio(false)
    14 #define TIE std::cin.tie(0)
    15 #define MIN2(a,b) (a<b?a:b)
    16 #define MIN3(a,b) (a<b?(a<c?a:c):(b<c?b:c))
    17 #define MAX2(a,b) (a>b?a:b)
    18 #define MAX3(a,b,c)  (a>b?(a>c?a:c):(b>c?b:c))
    19 typedef long long LL;
    20 typedef unsigned long long ULL;
    21 const int INF = 0x3f3f3f3f;
    22 const double PI = 4.0*atan(1.0);
    23 
    24 const int MAX_L = 305;
    25 int n, ans, x, y, t;
    26 int board[MAX_L][MAX_L];
    27 bool vis[MAX_L][MAX_L];
    28 int dx[4] = { 1, -1, 0, 0 }, dy[4] = { 0, 0, 1, -1 };
    29 typedef struct Point{
    30     int x, y, t;
    31     Point(int x = 0, int y = 0, int t = 0) :x(x), y(y), t(t){}
    32 }P;
    33 bool solve()
    34 {
    35     memset(vis, 0, sizeof(vis));
    36     queue<P> que;
    37     que.push(P(0, 0));
    38     vis[0][0] = true;
    39     while (que.size()){
    40         P p = que.front(); que.pop();
    41         if (board[p.x][p.y]==INF){
    42             ans = p.t; return true;
    43         }
    44         for (int i = 0; i < 4; i++){
    45             int nx = p.x + dx[i], ny = p.y + dy[i];
    46             if (0 <= nx && nx < MAX_L && 0 <= ny && ny<MAX_L
    47                 &&!vis[nx][ny] && board[nx][ny]>p.t + 1){
    48                 que.push(P(nx, ny, p.t + 1));
    49                 vis[nx][ny] = true;
    50             }
    51         }
    52     }
    53     return false;
    54 }
    55 int main()
    56 {
    57     while (~scanf("%d", &n)){
    58         memset(board, 0x3f, sizeof(board));
    59         for (int i = 0; i < n; i++){
    60             scanf("%d%d%d", &x, &y, &t);
    61             board[x][y] = MIN2(board[x][y],t);
    62             for (int i = 0; i < 4; i++){
    63                 int nx = x + dx[i], ny = y + dy[i];
    64                 if (0 <= nx && 0 <= ny){
    65                     board[nx][ny] = MIN2(board[nx][ny], t);
    66                 }
    67             }
    68         }
    69         if (board[0][0] == 0){ puts("-1"); continue;}
    70         printf("%d
    ", solve() ? ans : -1);
    71     }
    72 }
  • 相关阅读:
    文件操作小练习
    阶段练习1
    copy小练习
    小练习
    str 小列题
    条款50:使用自定义的new以及delete的时机会
    条款49:了解new-handle行为
    简单的说一下:tarits技法就是一种模板元编程,起可以将本来处于运行期的事拉到编译期来做,增加了运行效率。 看以非模板元编程的例子,就是前面的那个例子:
    条款47:请使用traits class表示类型信息
    条款46:需要类型转换的时候请为模板定义非成员函数
  • 原文地址:https://www.cnblogs.com/cumulonimbus/p/5861582.html
Copyright © 2011-2022 走看看