• HDU--1242--Rescue(BFS)


    Rescue

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


    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     Name: HDU--1242--Rescue
     3     Copyright: ©2017 日天大帝
     4     Author: 日天大帝
     5     Date: 22/04/17 19:18
     6     Description: BFS,第一次搜索一遍过,留文纪念
     7 */
     8 #include<iostream>
     9 #include<queue> 
    10 #include<cstring>
    11 using namespace std;
    12 struct node{
    13     int x,y,steps;
    14     node():steps(0){
    15     };
    16     bool operator<(const node &a)const {
    17         return steps>a.steps;
    18     }
    19 };
    20 int n,m;
    21 node s,e;
    22 int dir[4][2] = {0,1,0,-1,1,0,-1,0};
    23 char map[205][205];
    24 void bfs(){
    25     priority_queue<node> q;
    26     q.push(s);
    27     map[s.x][s.y] = '#';
    28     while(!q.empty()){
    29         node a,temp = q.top();q.pop();
    30         for(int i=0; i<4; ++i){
    31             a = temp;
    32             a.x += dir[i][0];
    33             a.y += dir[i][1];
    34             if(a.x<0||a.y<0||a.x>=n||a.y>=m|| map[a.x][a.y] == '#')continue;
    35             if(a.x == e.x&& a.y == e.y){
    36                 cout<<a.steps+1<<endl;return ;
    37             }
    38             if(map[a.x][a.y] == '.')a.steps++;
    39             else a.steps += 2;
    40             map[a.x][a.y] = '#';
    41             q.push(a);
    42         }
    43     }
    44     cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
    45 }
    46 int main(){
    47     ios::sync_with_stdio(false);
    48     
    49     while(cin>>n>>m){
    50         memset(map,0,sizeof(map));
    51         for(int i=0; i<n; ++i){
    52             cin>>map[i];
    53             for(int j=0; j<m; ++j){
    54                 if(map[i][j] == 'r'){
    55                     s.x = i;s.y = j;
    56                 }
    57                 if(map[i][j] == 'a'){
    58                     e.x = i;e.y = j;
    59                 }
    60             }
    61         }
    62         bfs();
    63     }
    64     return 0;
    65 }
  • 相关阅读:
    如何辨别护照的种类
    C#枚举中使用Flags特性
    那些年,我们一起学WCF--(7)PerSession实例行为
    64位系统使用Access 数据库文件的彻底解决方法
    从Excel中导入数据时,提示“未在本地计算机上注册“Microsoft.ACE.OLEDB.12.0”提供程序”的解决办法
    TortoiseSVN与VisualSVN Server搭建SVN版本控制系统
    解决Winform程序在不同分辨率系统下界面混乱
    【已解决】Https请求——基础连接已经关闭 发送时发生错误
    Entity Framework Code First学习系列目录
    PowerDesigner之PDM(物理概念模型)各种属性建立如PK,AK等
  • 原文地址:https://www.cnblogs.com/slothrbk/p/6748904.html
Copyright © 2020-2023  润新知