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

    http://poj.org/problem?id=3669

    题意:

    给定几个坐标,在这些坐标上 t 时刻会有陨石雨,上下左右也被损坏。怎样在最短的时间内找到一个安全的地方。

    思路:预处理每个陨石下落的周围的点,然后bfs就可以。

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <queue>
     4 #include <algorithm>
     5 #define maxn 50000
     6 using namespace std;
     7 
     8 int n;
     9 int ans,max1;
    10 int vis[400][400];
    11 int dir[5][2]={{0,0},{0,1},{0,-1},{1,0},{-1,0}};
    12 struct node
    13 {
    14     int x,y;
    15     int t;
    16 }p[maxn],st1,st2;
    17 
    18 void bfs(int x,int y)
    19 {
    20     queue<node>q;
    21     st1.x=x;
    22     st1.y=y;
    23     st1.t=0;
    24     if(vis[x][y]==0)
    25     {
    26         return;
    27     }
    28     q.push(st1);
    29     while(!q.empty())
    30     {
    31         st2=q.front(); q.pop();
    32         if(vis[st2.x][st2.y]==-1)
    33         {
    34             ans=st2.t;
    35             return ;
    36         }
    37         for(int i=0; i<5; i++)
    38         {
    39             int x1=st2.x+dir[i][0];
    40             int y1=st2.y+dir[i][1];
    41             if(!(x1>=0&&y1>=0)) continue;
    42             if((vis[x1][y1]>st2.t+1)||(vis[x1][y1]==-1))
    43             {
    44                 if(vis[x1][y1]!=-1)
    45                 {
    46                     vis[x1][y1]=st2.t+1;
    47                 }
    48                 st1.x=x1;st1.y=y1;
    49                 st1.t=st2.t+1;
    50                 q.push(st1);
    51             }
    52         }
    53     }
    54 }
    55 
    56 int main()
    57 {
    58     while(scanf("%d",&n)!=EOF)
    59     {
    60         ans=-1;
    61         memset(vis,-1,sizeof(vis));
    62         for(int i=1; i<=n; i++)
    63         {
    64             scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].t);
    65             for(int j=0; j<5; j++)
    66             {
    67                 int xx=p[i].x+dir[j][0];
    68                 int yy=p[i].y+dir[j][1];
    69                 if(!(xx>=0&&yy>=0)) continue;
    70                 if(vis[xx][yy]==-1)
    71                 {
    72                     vis[xx][yy]=p[i].t;
    73                 }
    74                 else
    75                 {
    76                     vis[xx][yy]=min(vis[xx][yy],p[i].t);
    77                 }
    78             }
    79         }
    80         bfs(0,0);
    81         printf("%d
    ",ans);
    82     }
    83     return 0;
    84 }
    View Code
  • 相关阅读:
    leetcode(js)算法605之种花问题
    如何使绝对定位内部元素不继承父级宽度,而是靠内容自动撑开宽度(转载)
    SQL连接查询、变量、运算符、分支、循环语句
    SQL主外键和子查询
    数据库函数
    数据库的备份恢复和部分操作语句
    SQL部分 数据库的建立 增删改查
    【转】毛玻璃特效
    Font Awesome符号字体
    form表单验证和事件
  • 原文地址:https://www.cnblogs.com/fanminghui/p/3993782.html
Copyright © 2011-2022 走看看