7 8 #.#####. #.a#..r. #..#x... ..#..#.# #...##.. .#...... ........
13
题解:广搜,每次找时间最短的一个,因为遇到士兵时间多1,此时这个点应该排在后面。所以用优先队列排序。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
struct Node
{
    int x;
    int y;
    int time;
    Node(int a,int b,int c)
    {
        x = a;
        y = b;
        time = c;
    }
    bool operator< (Node t) const
    {
        return time > t.time;
    }
};
int sx,sy;
int n,m;
int ans;
char map[205][205];
int d[4][2] = {{0,-1},{0,1},{-1,0},{1,0}};
bool bfs()
{
    priority_queue<Node> q;
    q.push(Node(sx,sy,0));
    ans = 0;
    map[sx][sy] = ‘#‘;
    while(!q.empty())
    {
        Node p = q.top();
        q.pop();
        for(int i = 0;i < 4;i++)
        {
            int x = p.x + d[i][0];
            int y = p.y + d[i][1];
            if(x >= 0 && x <  n && y >= 0 && y <  m && map[x][y] != ‘#‘)
            {
                if(map[x][y] == ‘a‘)
                {
                    ans = p.time + 1;
                    return true;
                }
                if(map[x][y] == ‘x‘)
                {
                    q.push(Node(x,y,p.time + 2));
                }
                else
                {
                    q.push(Node(x,y,p.time + 1));
                }
                map[x][y] = ‘#‘;
            }
        }
    }
    
    return false;
    
}
int main()
{
    while(scanf("%d%d",&n,&m) != EOF)
    {
        getchar();
        for(int i = 0;i < n;i++)
        {
            for(int j = 0;j < m;j++)
            {
                scanf("%c",&map[i][j]);
                if(map[i][j] == ‘r‘)
                {
                    sx = i;
                    sy = j;
                }
            }
            getchar();
        }
        if(bfs())
        {
            printf("%d\n",ans);    
        }
        else
        {
            printf("Poor ANGEL has to stay in the prison all his life.\n");
        }    
    }
    
    
    return 0;
}原文:http://www.cnblogs.com/yutingliuyl/p/6863812.html