zoukankan      html  css  js  c++  java
  • hdu1242 优先队列+bfs

    Rescue

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


    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:  1240 1016 1010 1072 1241 
     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <queue>
     4 #include <algorithm>
     5 using namespace std;
     6 
     7 struct Point
     8 {
     9     int x,y,t;
    10     bool operator < (const Point &a)const
    11     {
    12         return t>a.t;
    13     }
    14 };
    15 
    16 char map[205][205];
    17 Point start;
    18 int n,m;
    19 int diect[][2]={{1,0},{-1,0},{0,1},{0,-1}}; 
    20 
    21 int bfs()
    22 {    
    23     priority_queue<Point> que;
    24     Point cur,next;
    25     int i,j;
    26 
    27     map[start.x][start.y]='#';
    28     que.push(start);
    29     while(!que.empty())
    30     {
    31         cur=que.top();
    32         que.pop();
    33         for(i=0;i<4;i++)
    34         {
    35             next.x=cur.x+diect[i][0];  
    36             next.y=cur.y+diect[i][1];  
    37             next.t=cur.t+1;
    38             if(next.x<0 || next.x>=n || next.y<0 || next.y>=m)
    39                 continue;
    40             if(map[next.x][next.y]=='#')
    41                 continue;
    42             if(map[next.x][next.y]=='r')
    43                 return next.t;
    44             if(map[next.x][next.y]=='.')
    45             {
    46                 map[next.x][next.y]='#';
    47                 que.push(next);
    48             }
    49             else if(map[next.x][next.y]=='x')
    50             {
    51                 map[next.x][next.y]='#';
    52                 next.t++;
    53                 que.push(next);
    54             }
    55         }
    56     }
    57     return -1;
    58 }
    59 
    60 int main()
    61 {
    62     int i,j,ans;
    63     char *p;
    64     while(scanf("%d %d",&n,&m)!=EOF)
    65     {
    66         for(i=0;i<n;i++)
    67         {
    68             scanf("%s",map[i]);
    69             if(p=strchr(map[i],'a'))
    70             {
    71                 start.x=i;
    72                 start.y=p-map[i];
    73                 start.t=0;
    74             }
    75         }
    76         ans=bfs();
    77         if(ans==-1)
    78             printf("Poor ANGEL has to stay in the prison all his life.
    ");
    79         else
    80             printf("%d
    ",ans);
    81     }
    82     return 0;
    83 }
    View Code
  • 相关阅读:
    scala言语基础学习七
    scala言语基础学习六
    scala言语基础学习五
    scala言语基础学习四
    scala言语基础学习三(面向对象编程)
    scala言语基础学习三
    scala言语基础学习二
    scala言语基础学习
    并发编程实战的阅读(锁的重入)
    数据库必会必知 之 SQL四种语言:DDL DML DCL TCL(转)
  • 原文地址:https://www.cnblogs.com/cyd308/p/4817740.html
Copyright © 2011-2022 走看看