Rescue

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
题意:在迷宫里拯救朋友.是路#是墙a是哨兵,只能向前后左右移动,移动一次时间加一,遇到哨兵要再加一个时间;
解题思路:优先队列加广搜;
感悟:广搜还是不大熟练,写着写着就不知道怎么写了,放假得好好写写!!;
代码:
#include
#include
#include
#include
#define maxn 205
using namespace std;
struct Fr
{
    int x,y,t;//分别表示朋友的坐标和走的步数;
    bool operator<(const Fr&other)  const
    {
        return this->t>other.t;
    }
};
int visit[maxn][maxn];//这个是记录步数的不是记录走没走的不能用布尔型
char mapn[maxn][maxn];
int n,m,direction[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int x1,y1,x2,y2;
int judge(int x,int y)
{
    if(x<1||x>n||y<1||y>m||!visit[x][y]||mapn[x][y]=='#')
        return 1;
    else
        return 0;
}
int bfs()
{
    priority_queue Q;
    Fr fr,next_fr;
    fr.x=x1;
    fr.y=y1;
    fr.t=0;
    //printf("x1=%d x2=%d ",x1,x2);
    Q.push(fr);
    visit[x1][y1]=0;
    while(!Q.empty())
    {
        //printf("fr.x=%d x2=%d ",fr.x,x2);
        //printf("fr.y=%d y2=%d ",fr.y,y2);
        fr=Q.top();
        Q.pop();
        if(fr.x==x2&&fr.y==y2)
            return fr.t;//终止条件
        for(int i=0;i<4;i++)
        {
            next_fr=fr;
            next_fr.x+=direction[i][0];
            next_fr.y+=direction[i][1];
            if(judge(next_fr.x,next_fr.y))
                continue;
            next_fr.t++;
            if(mapn[next_fr.x][next_fr.y]=='x')
                next_fr.t++;//有哨兵得再加一次;
            if(visit[next_fr.x][next_fr.y]>=next_fr.t)
            {
                visit[next_fr.x][next_fr.y]=next_fr.t;
                Q.push(next_fr);
            }
        }
    }
    return 0;

}
int main()
{
    //freopen("in.txt", "r", stdin);
    while(~scanf("%d%d ",&n,&m))
    {
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                scanf("%c",&mapn[i][j]);
                if(mapn[i][j]=='r')
                {
                    x1=i;
                    y1=j;//朋友的位置
                }
                if(mapn[i][j]=='a')
                {
                    x2=i;
                    y2=j;//这个人的位置
                }
            }
            scanf(" ");
        }
        memset(visit,1,sizeof(visit));
        //for(int i=1;i<=n;i++)
        //{
        //    for(int j=1;j<=m;j++)
        //    {
        //        printf("%c",mapn[i][j]);
        //    }
        //    printf(" ");
        //}
        int ans=0;
        ans=bfs();
        if(ans)
            printf("%d ",ans);
        else
            printf("Poor ANGEL has to stay in the prison all his life. ");
    }
}
原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/5781621.html