• 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 }
  • 相关阅读:
    Linux用户态程序计时方式详解
    我的代码重构经验
    基于链表的C语言堆内存检测
    C语言内存使用的常见问题及解决之道
    1151 LCA in a Binary Tree (30 分)
    1150 Travelling Salesman Problem (25 分)
    1149 Dangerous Goods Packaging (25 分)
    1148 Werewolf
    1155 Heap Paths (30 分)
    1154 Vertex Coloring (25 分)
  • 原文地址:https://www.cnblogs.com/langyao/p/6748904.html
Copyright © 2020-2023  润新知