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

    Meteor Shower
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 12816   Accepted: 3451

    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
    题意思路:m个陨石将砸向第一象限和正坐标抽,每块陨石砸的时间分别为Ti,从0,0出发,如何走才能达到安全位置;
    用一个T[][]数组记录每个点的最早爆炸时间,vis标记该点是否被砸,如果该点不可能被砸,那么为安全位置,否则向四个方向扩展,改点可走的条件为改点没有走过以及该点爆炸的时间大于当前时间,对于走过的点标记T[][]为0,那么改点在以后就不可能再走。

    反正我的思路好麻烦,每次都要写个函数对所有陨石进行遍历判断是否为安全位置。
    AC:
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <cmath>
     4 #include <cstdlib>
     5 #include <algorithm>
     6 #include <queue>
     7 #include <stack>
     8 #include <map>
     9 #include <set>
    10 #include <vector>
    11 #define INF 0x3f3f3f
    12 #define eps 1e-8
    13 #define MAXN (50000+1)
    14 #define MAXM (100000)
    15 #define Ri(a) scanf("%d", &a)
    16 #define Rl(a) scanf("%lld", &a)
    17 #define Rf(a) scanf("%lf", &a)
    18 #define Rs(a) scanf("%s", a)
    19 #define Pi(a) printf("%d
    ", (a))
    20 #define Pf(a) printf("%.2lf
    ", (a))
    21 #define Pl(a) printf("%lld
    ", (a))
    22 #define Ps(a) printf("%s
    ", (a))
    23 #define W(a) while(a--)
    24 #define CLR(a, b) memset(a, (b), sizeof(a))
    25 #define MOD 1000000007
    26 #define LL long long
    27 #define lson o<<1, l, mid
    28 #define rson o<<1|1, mid+1, r
    29 #define ll o<<1
    30 #define rr o<<1|1
    31 using namespace std;
    32 struct Node{
    33     int x, y, step;
    34 };
    35 bool judge(int x, int y){
    36     return x >= 0 && y >= 0;
    37 }
    38 bool vis[301][301];
    39 int Move[4][2] = {0,1, 0,-1, 1,0, -1,0};
    40 int T[301][301];
    41 int BFS(int x, int y)
    42 {
    43     if(!vis[x][y])
    44         return 0;
    45     queue<Node> Q;
    46     Node now, next;
    47     now.x = x; now.y = y; now.step = 0;
    48     Q.push(now);
    49     while(!Q.empty())
    50     {
    51         now = Q.front();
    52         Q.pop();
    53         if(!vis[now.x][now.y])
    54             return now.step;
    55         for(int k = 0; k < 4; k++)
    56         {
    57             next.x = now.x + Move[k][0];
    58             next.y = now.y + Move[k][1];
    59             next.step = now.step + 1;
    60             if(!judge(next.x, next.y)) continue;
    61             if(vis[next.x][next.y])
    62             {
    63                 if(T[next.x][next.y] != INF && next.step < T[next.x][next.y])
    64                 {
    65                     T[next.x][next.y] = 0;
    66                     Q.push(next);
    67                 }
    68             }
    69             else
    70                 return next.step;
    71         }
    72     }
    73     return -1;
    74 }
    75 int main()
    76 {
    77     int n;
    78     while(Ri(n) != EOF)
    79     {
    80         CLR(vis, false); CLR(T, INF);
    81         for(int i = 0; i < n; i++)
    82         {
    83             int x, y, t;
    84             Ri(x); Ri(y); Ri(t);
    85             T[x][y] = min(T[x][y], t);
    86             vis[x][y] = true;
    87             for(int k = 0; k < 4; k++)
    88             {
    89                 int xx = x + Move[k][0];
    90                 int yy = y + Move[k][1];
    91                 if(!judge(xx, yy)) continue;
    92                 vis[xx][yy] = true;
    93                 T[xx][yy] = min(T[xx][yy], t);
    94             }
    95         }
    96         Pi(BFS(0, 0));
    97     }
    98     return 0;
    99 }

    WA:

      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <algorithm>
      5 #include <queue>
      6 using namespace std;
      7 int vis[305][305],b[305][305];
      8 struct node {int x,y,t;}m[50005];
      9 bool cmp(node a,node b)    {return a.t<=b.t;}
     10 int n,Mint;
     11 int dx[]={0,0,0,1,-1},dy[]={0,1,-1,0,0};
     12 bool safe(node p)
     13 {
     14     int l=-1,r=n,mid;
     15     while(r-l>1)
     16     {
     17         mid=(l+r)/2;
     18         if(m[mid].t>=p.t)
     19             r=mid;
     20         else
     21             l=mid;
     22     }
     23     for(int i=r;i<n;i++)
     24     {
     25         for(int j=0;j<5;j++)
     26         {
     27             if(p.x==(m[i].x+dx[j])&&p.y==(m[i].y+dy[j]))
     28                 return 0;
     29         }
     30     }
     31     return 1;
     32 }
     33 int bfs()
     34 {
     35     int i,j;
     36     Mint=-1;
     37     queue<node> s;
     38     node temp,next;
     39     temp.x=temp.y=temp.t=0;
     40     s.push(temp);
     41     while(!s.empty())
     42     {
     43         temp=s.front();
     44         s.pop();
     45         if(safe(temp))
     46         {
     47             Mint=temp.t;
     48             break;
     49         }
     50         temp.t++;
     51         int l=-1,r=n,mid;
     52         while(r-l>1)
     53         {
     54             mid=(l+r)/2;
     55             if(m[mid].t>=temp.t)
     56                 r=mid;
     57             else
     58                 l=mid;
     59         }
     60         while(m[r].t==temp.t)
     61         {
     62 
     63             for(int i=0;i<=4;i++)
     64                 b[m[r].x+dx[i]][m[r].y+dy[i]]=1;
     65             r++;
     66         }
     67         for(i=1;i<=4;i++)
     68         {
     69             next.x=temp.x+dx[i];
     70             next.y=temp.y+dy[i];
     71             next.t=temp.t;
     72             if(next.x>=0&&next.y>=0&&vis[next.x][next.y]==0&&b[next.x][next.y]==0)
     73             {
     74                 vis[next.x][next.y]=1;
     75                 s.push(next);
     76             }
     77             //cout<<"ads";
     78         }
     79     }
     80     return Mint; 
     81 }
     82 int main()
     83 {
     84     int i,j;
     85     freopen("in.txt","r",stdin);
     86     while(scanf("%d",&n)!=EOF)
     87     {
     88         memset(vis,0,sizeof(vis));
     89         memset(b,0,sizeof(vis));
     90         for(i=0;i<n;i++)
     91             scanf("%d%d%d",&m[i].x,&m[i].y,&m[i].t);
     92         sort(m,m+n,cmp);
     93         //for(i=0;i<n;i++)
     94         //    cout<<m[i].x<<" "<<m[i                                                                                                                                                                                                                                                                                                                                                                                    y<<" "<<m[i].t<<endl;
     95         i=0;
     96         bool flag=0;
     97         while(m[i].t==0)
     98         {
     99             if((m[i].x==0&&m[i].y==0)||(m[i].x==0&&m[i].y==1)||(m[i].x==1&&m[i].y==0))
    100             {
    101                 cout<<-1<<endl;
    102                 flag=1;
    103                 break;
    104             }
    105             i++;
    106         }
    107         if(flag)
    108             continue;
    109         bfs();
    110         printf("%d
    ",Mint);
    111     }
    112 }
  • 相关阅读:
    变量对象,作用域链,闭包,匿名函数,this关键字,原型链,构造器,js预编译,对象模型,执行模型,prototype继承
    iptables-snat-dnat-设置
    salt-ssh
    linux-vsftp
    网站申请HTTPS 访问
    shell 处理文件脚本
    last与lastb命令 读取的日志文件
    Linux-server-sshd
    Nginx 日志切割
    修改或隐藏服务器名称需要修改源码
  • 原文地址:https://www.cnblogs.com/a1225234/p/5150559.html
Copyright © 2011-2022 走看看