HDU 1242 Rescue(BFS优先队列)

Rescue

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



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

题解:大意为天使被魔头抓走了,关在了监狱内,监狱为二维字符数组,其中,r为营救者,a为天使,x为警卫,#为墙,营救者每走一格花费一分钟,且它足够强大可以杀死所有的守卫,但必须多花费一分钟……
用优先队列来做……


#include
#include
#include
#include
#include
#include
#include
#include;
#include;
#include 
#include 
#define mem(a) memset(a,0,sizeof(a))
using namespace std;
char map[201][201];                          /*储存监狱……*/
bool v[201][201];                            /*标记……*/
int d[4][2]={{1,0},{-1,0},{0,1},{0,-1}};     /*营救者的四种走法……*/
int n,m,a,b,aa,bb;
struct node                                  /*定义优先队列*/
{
    int x,y,s,time;
    bool operator<(const node &z)const
    {
        return time>z.time;
    }
};
int check(int x,int y)                           /*判断此时的坐标是否可走*/
{
    if(x>=0&&x=0&&yq;                     /*优先队列……*/
    node start,next;
    start.x=a,start.y=b,start.time=0;
    q.push(start);
    while(!q.empty())
    {
        start=q.top();
        q.pop();
        if(start.x==aa&&start.y==bb){printf("%d\n",start.time);return;}     /*找到,结束*/
        for(int i=0;i<4;i++)
        {
            next.x=start.x+d[i][0],next.y=start.y+d[i][1];
            if(check(next.x,next.y))
            {
                if(map[next.x][next.y]=='x')
                    next.time=start.time+2;
                else
                    next.time=start.time+1;
                v[next.x][next.y]=true;
                q.push(next);
            }
        }
    }
    printf("Poor ANGEL has to stay in the prison all his life.\n");return;   /*未找到……*/
}
int main()
{
    while(~scanf("%d%d",&n,&m)&&(n||m))
    {
        getchar();
        mem(v);
        for(int i=0;i






你可能感兴趣的:(搜索,BFS)