hdu 题目1242 Rescue (BFS)

Rescue

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 80   Accepted Submission(s) : 39

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

 

 

 

 

第一次使用priority_queue<>Q; 

需要设置优先级

 friend bool operator< (node n1,node n2){
        return n2.step < n1.step;//注意这里大小关系
    }

 

 

题目需要从目标地址‘a’ 开始四个方向广搜,(因为‘r’) 可能有好多个,只要搜到 r 则停止返回搜索步数

注意步数的加减多少,,,   ‘x’ 要加 2 ,用到优先队列,将路径最短的作为对头出队

 

 


 

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
using namespace std;

char map[211][211];
int n,m,s_x,s_y,e_x,e_y;

struct node{
    int x,y;
    int step;
    friend bool operator< (node n1,node n2){
        return n2.step < n1.step;
    }
};
int dir[4][2]={1,0,-1,0,0,1,0,-1};

int ok(int x,int y)
{
    if(x<0 || x>=n || y<0 || y>=m || map[x][y]==1)    return 0;
    return 1;
}
    
bool bfs(){
    
    priority_queue <node> q;
    node cur,next;
    int i;
    
    cur.x = s_x;
    cur.y=s_y;
    cur.step =0;
    map[cur.x][cur.y] = 1;
    
    q.push(cur);
    while(!q.empty()){
        
        cur = q.top();
        q.pop();
        if(cur.x== e_x && cur.y== e_y) {
            printf("%d\n",cur.step);
            return 1;                    
        }
        for(i=0;i<4;i++){
            next.x = cur.x + dir[i][0];
            next.y = cur.y + dir[i][1];
            
            if(ok(next.x,next.y)){
                if(map[next.x][next.y]==-1) next.step = cur.step+2;
                else next.step = cur.step+1;
                map[next.x][next.y] = 1;
                q.push(next);    
            }                            
        }
    }
    return 0;
}
int main()
{
    int i,j;
    char s[211];    
     while(scanf("%d%d",&n,&m)!=-1)
    {
        memset(map,0,sizeof(map));


        for(i=0;i<n;i++)
        {
            scanf("%s",s);
            for(j=0;s[j];j++)
            {
                if(s[j]=='r')    {s_x=i;s_y=j;}
                else if(s[j]=='a'){e_x    =i;e_y=j;}
                else if(s[j]=='#')map[i][j]=1;
                else if(s[j]=='.')map[i][j]=0;
                else if(s[j]=='x')map[i][j]=-1;
            }
        }
        if(!bfs())    printf("Poor ANGEL has to stay in the prison all his life.\n" );    
    }
    return 0;
}