zoukankan      html  css  js  c++  java
  • HDU1242 Rescue(BFS+优先队列)

    Rescue

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 20938    Accepted Submission(s): 7486


    Problem Description
    Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison.

    Angel's friends want to save Angel. Their task is: approach Angel. We assume that "approach Angel" is to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.

    You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.)
     

     

    Input
    First line contains two integers stand for N and M.

    Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and "r" stands for each of Angel's friend.

    Process to the end of the file.
     

     

    Output
    For each test case, your program should output a single integer, standing for the minimal time needed. If such a number does no exist, you should output a line containing "Poor ANGEL has to stay in the prison all his life."
     

     

    Sample Input
    7 8
    #.#####.
    #.a#..r.
    #..#x...
    ..#..#.#
    #...##..
    .#......
    ........
     

     

    Sample Output
    13
    题意:问最小花时间从起点到终点,其中要到守卫的地方要多花1个时间。
    思路:由于其中有若干步要花费2个时间,可以用优先队列存储,走花费时间少的。用DFS枚举的会超时。
    收获:优先队列定义结构体优先级。
     1 #include<cstdio>
     2 #include<cstring>
     3 #include<iostream>
     4 #include<algorithm>
     5 #include<queue>
     6 using namespace std;
     7 #define maxn 200+10
     8 int n,m,flag;
     9 struct Node
    10 {
    11     int x,y,cost;
    12     bool operator < (const Node &a)const//定义时间花费少的优先级高。
    13     {
    14         return cost>a.cost;
    15     }
    16 };
    17 Node st,et,f,k;
    18 int dx[]={0,0,-1,1};
    19 int dy[]={1,-1,0,0};
    20 int vis[maxn][maxn];
    21 char map[maxn][maxn];
    22 void bfs()
    23 {
    24     priority_queue<Node>q;
    25     q.push(st);
    26     while(!q.empty())
    27     {
    28         f=q.top();
    29         for(int i=0;i<4;i++)
    30         {
    31             k.x=f.x+dx[i];
    32             k.y=f.y+dy[i];
    33             if(!vis[k.x][k.y]&&k.x>=0&&k.x<n&&k.y>=0&&k.y<m)
    34             {
    35                 vis[k.x][k.y]=1;
    36                 k.cost=f.cost+1;
    37                 if(map[k.x][k.y]=='x')
    38                     k.cost++;
    39                 if(k.x==et.x&&k.y==et.y)
    40                     {
    41                     printf("%d
    ",k.cost);
    42                     return;
    43                     }
    44                 q.push(k);
    45             }
    46             }
    47             q.pop();
    48     }
    49     printf("Poor ANGEL has to stay in the prison all his life.
    ");
    50 }
    51 
    52 int main()
    53 {
    54     while(~scanf("%d%d",&n,&m))
    55     {
    56         if(m==0)
    57             break;
    58         memset(vis,0,sizeof(vis));
    59         getchar();
    60         for(int i=0;i<n;i++)
    61         {
    62             for(int j=0;j<m;j++)
    63             {
    64                 scanf("%c",&map[i][j]);
    65                 if(map[i][j]=='#')
    66                 {
    67                     vis[i][j]=1;
    68                 }
    69                 if(map[i][j]=='r')
    70                 {
    71                      st.x=i;
    72                      st.y=j;
    73                      st.cost=0;
    74                 }
    75                 if(map[i][j]=='a')
    76                 {
    77                     et.x=i;
    78                     et.y=j;
    79                 }
    80 
    81             }
    82             getchar();
    83         }
    84         bfs();
    85     }
    86 
    87     return 0;
    88 }
  • 相关阅读:
    利用UltraScale和UltraScale+FPGA和MPSOC加速DSP设计生产力
    ARM系列处理器和架构
    Thumb扩展
    使用Redis分布式锁处理并发,解决超卖问题
    idea指定启动参数、环境变量
    Json返回结果为null属性不显示解决
    Spring Cloud Zuul 网关服务的fallback
    记录一次URL中有特殊字符怎么处理?
    logback的使用和logback.xml详解
    通过gitlab的webhook触发Jenkins自动构建设置
  • 原文地址:https://www.cnblogs.com/ZP-Better/p/4698762.html
Copyright © 2011-2022 走看看