zoukankan      html  css  js  c++  java
  • hdu 1242 Rescue (BFS)

    Rescue

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


    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
     
    Author
    CHEN, Xue
     
    Source
     
    Recommend
    Eddy   |   We have carefully selected several similar problems for you:  1072 1175 1026 1180 1401 
     
     1 //31MS    356K    1410 B    C++
     2 /*
     3 
     4     题意:
     5         从点a到点r,求最短步数
     6     
     7     BFS:
     8         典型的BFS,这题数据有点怪.最好用优先队列实现bfs。 
     9     
    10 */
    11 #include<iostream>
    12 #include<cstdio>
    13 #include<queue>
    14 using namespace std;
    15 int mov[4][2]={0,1,1,0,-1,0,0,-1};
    16 char map[202][202];
    17 int bx,by,n,m;
    18 struct node
    19 {
    20     int x,y,step;
    21     friend bool operator < (node a,node b)
    22     {
    23         return a.step>b.step;
    24     }
    25 };
    26 void bfs(int x,int y)
    27 {
    28      priority_queue <node> Q;
    29      node t={x,y,0};
    30      Q.push(t);
    31      while(!Q.empty())
    32      {
    33          t=Q.top();
    34          Q.pop();
    35          for(int i=0;i<4;i++)
    36          {
    37              node tt=t;
    38              tt.x+=mov[i][0];
    39              tt.y+=mov[i][1];
    40              tt.step++;
    41              if(tt.x>=0&&tt.x<n&&tt.y>=0&&tt.y<m&&map[tt.x][tt.y]!='#')
    42              {
    43                  if(map[tt.x][tt.y]=='r')
    44                  {
    45                      cout<<tt.step<<endl;
    46                      return;
    47                  }
    48                  else if(map[tt.x][tt.y]=='x')
    49                     tt.step++;
    50                  map[tt.x][tt.y]='#';
    51                  Q.push(tt);
    52              }
    53          }
    54      }
    55      cout<<"Poor ANGEL has to stay in the prison all his life.
    ";
    56 }
    57 int main(void)
    58 {
    59     int i,j;
    60     while(scanf("%d%d",&n,&m)!=EOF)
    61     {
    62         for(i=0;i<n;i++)
    63             for(j=0;j<m;j++)
    64             {
    65                 cin>>map[i][j];
    66                 if(map[i][j]=='a')
    67                     bx=i,by=j;
    68             }
    69         bfs(bx,by);
    70     }
    71     return 0;
    72 }
    73                 
    74                 
    75                 
  • 相关阅读:
    简述拦截器的工作原理?
    线程安全与非线程安全集合说一下,底层怎么实现的(hashmap,concurrenthashmap)
    表与表之间的关联关系
    手写9x9乘法表,冒泡排序
    主键和外键的区别
    为什么要使用连接池?
    AXI协议中的模棱两可的含义的解释(Cachable和Bufferable)
    ahb时序解析
    amba web
    Register Abstraction(9)
  • 原文地址:https://www.cnblogs.com/GO-NO-1/p/3465957.html
Copyright © 2011-2022 走看看