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): 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 }
  • 相关阅读:
    Redis集群的搭建
    CAS部署在Windows上
    Loadrunner中Error-26612HTTP Status-Cod
    Coneroller执行时候的-26374及-26377错误
    Loadrunner 26377错误
    歌名也好笑
    loadrunner 中Error和failed transaction 的区别
    loadrunner 性能测试报error-27796的解决
    lr11.0负载测试 real-world schedule 与basic schedule的区别是什么
    LR中错误代号为27796的一个解决方法
  • 原文地址:https://www.cnblogs.com/slothrbk/p/6748904.html
Copyright © 2011-2022 走看看