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                 
  • 相关阅读:
    SAP C4C OBN(Object Based Navigation)不能工作的原因分析
    使用SAP C4C自定义BO association创建动态下拉列表
    如何使用SAP HANA Studio的PlanViz分析CDS view性能问题
    如何使用jMeter测试SAP OData服务并发访问时的性能
    OData服务在SAP CRM,Cloud for Customer和S/4HANA上的实现比较
    SAP UI5和Vue的数据双向绑定实现原理比较
    在SAP WebClient UI里显示倒数计时的UI
    【年度重磅】2020华为云社区年度技术精选合集,700页+免费下载!
    面试必问:如何实现Redis分布式锁
    聊聊架构模式的变迁:从分层架构到微服务架构
  • 原文地址:https://www.cnblogs.com/GO-NO-1/p/3465957.html
Copyright © 2011-2022 走看看