Rescue(HDU-1242)

Problem Description

    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可能不止一个,因此从朋友找天使的角度来说较为繁琐,故以天使找朋友的角度来搜索即可。

Source Program

#include
#include
#include
using namespace std;

int n,m;
char maps[201][201];
int used[201][201];
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};//方向数组

struct node
{
    int x;
    int y;
    int step;
}start,endd;

bool judge(int x,int y)//越界判断
{
    if(x<0||y<0||x>=n||y>=m||used[x][y]||maps[x][y]=='#')
        return false;
    else
        return true;
}
void bfs()
{
    int i;
    queue  q;
    used[start.x][start.y]=1;
    q.push(start);//初始数据入队

    while(!q.empty())
    {
        start=q.front();
        q.pop();
        if(maps[start.x][start.y]=='r')//营救到天使,结束搜索
        {
            cout<>n>>m)
    {
        memset(used,0,sizeof(used));//地图清零
        for(i=0;i>maps[i][j];
                if(maps[i][j]=='a')//记录起始位置
                {
                    start.x=i;
                    start.y=j;
                    start.step=0;
                }
            }
        }
        bfs();
    }
    return 0;
}

 

你可能感兴趣的:(#,HDU,#,图论——图的搜索)