题目链接:https://cn.vjudge.net/contest/292780#problem/E
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1242
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

题意:
天使被囚禁在N*M矩阵的监狱,监狱里使的朋友想要拯救天使。
他们的任务是:接近天使。“接近天使”是为了达到天使留下的位置。当网格中有一名后卫时,我们必须杀死他(或她?)才能进入网格。我们假设我们向上,向下,向右,向左移动需要1个单位时间,并且杀死一名守卫也需要1个单位时间。我们足够强大,可以杀死所有的守卫。
“."代表道路,“a”代表Angel,“r”代表Angel的每个朋友,"x"代表守卫。

解题思路:
首先,我们马上就会想到bfs,确实搜索可以找到,但这题的一个条件选择的是需要消耗最少时间的那条路,所以,我们需要用优先队列,这样就可以确保输出的是最短时间。

如果不知什么是优先队列,可以看看我的另一个博客:我是博客

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<vector>
#include<queue>
using namespace std;
const int maxn = 200 + 10;
char mp[maxn][maxn];
int vis[maxn][maxn];
int n, m;
struct node//优先队列
{
    int x, y, step;
    friend bool operator<(node a, node b)
    {
    return a.step > b.step;//步数
    }
}; 
int d[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
void BFS(int x1, int y1, int x2, int y2)
{
    memset(vis, 0, sizeof(vis));
    priority_queue<node> que;//优先队列
    node e1, e2;
    e1.x = x1, e1.y = y1, e1.step = 0; // 起点
    que.push(e1);
    vis[x1][y1] = 1; //标记已经走过
    int ans = -1;
    while (!que.empty())
    {
        e1 = que.top();
        que.pop();
        if (e1.x == x2 && e1.y == y2)
        {
            ans = e1.step;
            break;
        }
        for (int i = 0; i < 4; i++)
        {
            e2.x = e1.x + d[i][0];
            e2.y = e1.y + d[i][1];
            if (e2.x < 0 || e2.x >= n || e2.y < 0 || e2.y >= m||vis[e2.x][e2.y]||mp[e2.x][e2.y] == '#') continue;
            if (mp[e2.x][e2.y] == 'x') e2.step = e1.step + 2;//杀要守卫多一秒
            else e2.step = e1.step + 1;
            que.push(e2);
            vis[e2.x][e2.y] = 1;
        }
    }
    if (ans==-1) 
    cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
    else
    printf("%d\n",ans);
}
int main()
{
    while(~scanf("%d%d",&n,&m)){
    for(int i=0;i<n;i++)
    {
        getchar();
        cin>>mp[i];
    }
    int q1,w1,q2,w2;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(mp[i][j]=='r')
            {
                q1=i;
                w1=j;
            }
            if(mp[i][j]=='a')
            {
                q2=i;
                w2=j;
            }
        }
    }
        BFS(q2, w2, q1, w1);
    }
    return 0;
}