HDU-1242-Rescue

Rescue

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

 

题意:点表示路,#表示墙,x表示守卫,a表示目的地,r为起点(起点可能不只有一个),只能玩上下左右四个方向走,每一步一单位时间,碰上守卫需要多花费一单位时间。问最少多长时间能到达。(也有可能不能到达)

注意:起点不止一个可以从终点搜索。


注意:正确做法是优先队列,每一次出队都是出时间最少的,这样可以保证每一步最优,并且一旦搜到目标即可立刻结束。

优先队列(priority_queue)的基本操作:

empty(); 队列为空返回1

pop();   出队

push();  入队

top();   返回队列中优先级最高的元素

size();  返回队列中元素的个数

 

 

 

#include

#include

#include

#include

#include

using namespace std;

char c[205][205];

bool mark[205][205];//标记这个点用没用过

int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}}; //四个方向

int ans;

int n,m;

struct node

{

    int x,y;

    int time;

    friend bool operator < (const node &a,const node &b)

    {

        return a.time>b.time;

    }

};

int go(int x,int y)

{

    if(0<=x&&x可以前进

        return 1;

    return 0;

}

int bfs(int x,int y)

{

    int i;

    node st,ed;//开始、结束

    priority_queueque;  //定义一个优先队列

    memset(mark,0,sizeof(mark));//标记清零

    st.x=x;

    st.y=y;

    st.time=0;

    mark[st.x][st.y]=1;

    que.push(st);

    while(!que.empty())

    {

        st=que.top();

        que.pop();

        if(c[st.x][st.y]=='r')//找到朋友

            return st.time;

        for(i=0;i<4;i++)

        {

            ed.x=st.x+dir[i][0];

            ed.y=st.y+dir[i][1];  //加上四个方向的坐标

            if(go(ed.x,ed.y)&&!mark[ed.x][ed.y])

            {

                mark[ed.x][ed.y]=1;

                if(c[ed.x][ed.y]=='x')

                    ed.time=st.time+2;

                else

                    ed.time=st.time+1;

                que.push(ed);

            }

        }

    }

    return -1;

}

int main()

{

    int x,y;

    while(~scanf("%d%d",&n,&m))

    {

        for(int i=0;i

        {

            scanf("%s",c[i]);

            for(int j=0;j

            {

                if(c[i][j]=='a')

                {

                    x=i;y=j;//从公主的位置开始找 即起点坐标

                }

            }

        }

        ans=bfs(x,y);

        if(ans==-1)

            printf("Poor ANGEL has to stay in the prison all his life.\n");

        else

            printf("%d\n",ans);

    }

    return 0;

}

你可能感兴趣的:(BFS,DFS,hdu)