• 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                 
  • 相关阅读:
    call_user_func和call_user_func_array的区别
    25行实现文件上传功能(PHP)
    PHP 结合MYSQL简单的实现了Todo List 功能
    向ASP.NET自定义控件中嵌入CSS资源
    模态子窗口不执行page_load
    oracle一次插入多条数据
    转载:数据库表结构设计方法及原则
    Js中的window.parent ,window.top,window.self
    在一个JS文件中包含中文字符串通过innerHTML输出后中文乱码
    转载:数据库表结构设计方法及原则
  • 原文地址:https://www.cnblogs.com/GO-NO-1/p/3465957.html
Copyright © 2020-2023  润新知