• Rescue(bfs)


    Rescue

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
    Total Submission(s) : 24   Accepted Submission(s) : 11
    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
     题解:
    没看见找不到还要输出字,错了n次,这就是没读清题就开始做题的后果。。。还有是遇见士兵要step多加一
    代码
     1 #include<stdio.h>
     2 #include<queue>
     3 using namespace std;
     4 struct Node{
     5     int nx,ny,step;
     6     friend bool operator < (Node a,Node b){
     7         return a.step>b.step;
     8     }
     9 };
    10 Node a,b;
    11 char map[250][250];
    12 int disx[5]={0,1,-1,0};
    13 int disy[5]={1,0,0,-1};
    14 int N,M,sx,sy,ex,ey,minstep,flot;
    15 void bfs(){
    16     priority_queue<Node>dl;
    17     a.nx=sx;a.ny=sy;a.step=0;
    18     dl.push(a);
    19     while(!dl.empty()){
    20         a=dl.top();
    21         dl.pop();
    22         map[a.nx][a.ny]='#';
    23         if(a.nx==ex&&a.ny==ey){flot=1;
    24             minstep=a.step;
    25             return;
    26         }
    27         for(int i=0;i<4;i++){
    28             b.nx=a.nx+disx[i];b.ny=a.ny+disy[i];b.step=a.step+1;
    29             if(b.nx<0||b.ny<0||b.nx>=N||b.ny>=M||map[b.nx][b.ny]=='#')continue;
    30             if(map[b.nx][b.ny]=='.'||map[b.nx][b.ny]=='r')dl.push(b);
    31             else{
    32                 b.step++;dl.push(b);
    33             }
    34         }
    35     }
    36 } 
    37 int main(){
    38     while(~scanf("%d%d",&N,&M)){minstep=0;flot=0;
    39         for(int i=0;i<N;i++)scanf("%s",map[i]);
    40         for(int x=0;x<N;x++){
    41             for(int y=0;y<M;y++){
    42                 if(map[x][y]=='a')sx=x,sy=y;
    43                 if(map[x][y]=='r')ex=x,ey=y;
    44             }
    45         }
    46         bfs();
    47         if(flot)printf("%d
    ",minstep);
    48         else puts("Poor ANGEL has to stay in the prison all his life.");
    49     }
    50     return 0;
    51 }
  • 相关阅读:
    技术晨读_2015_11_29
    mysql的timeout
    Gradle目录解析
    flexbox简介
    elasticsearch 查询(match和term)
    内存那些事
    elasticsearch 文档
    elasticsearch 集群
    elasticsearch中的API
    小菜的程序员道路(三)
  • 原文地址:https://www.cnblogs.com/handsomecui/p/4706225.html
Copyright © 2020-2023  润新知