hdu 1242Rescue (优先队列+bf)s

Rescue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 38884    Accepted Submission(s): 13423


 

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

 

 

Author

CHEN, Xue

 

 

Source

ZOJ Monthly, October 2003

 

 

Recommend

Eddy

注意,步数有两种,可以走一步,也可以消灭守卫走两步

方法一:普通队列,从朋友开始搜,把所有朋友入队,遇到状态小的就更新,足以最后那个边界返回,我一开始直接按找最小路径那样的返回,结果wa了

#include
#include
#include
#include
#include
using namespace std;
#define maxn 500
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int n,m;
struct node
{
    int x,y,dis;
    node(int x,int y,int dis):x(x),y(y),dis(dis)
    {

    }
};
int dis[maxn][maxn];
char mp[maxn][maxn];
//int sx,sy;
int ex,ey;
queueq;
int bfs()
{
    memset(dis,-1,sizeof(dis));
    while(!q.empty())
    {node u=q.front();
        q.pop();
        for(int i=0;i<4;i++)
        {
            int xx=dx[i]+u.x;
            int yy=dy[i]+u.y;
       int dist=u.dis+1;
            if(xx<0||xx>=n||yy<0||yy>=m||mp[xx][yy]=='#')
                continue;
                if(mp[xx][yy]=='x')
                    dist=dist+1;

            if(dis[xx][yy]==-1||(dist

方法二:优先队列,从天使开始,根据步数排序,每次找步数最小,就转化成最小路径问题了,这样做更优

#include
#include
#include
#include
#include
#define maxn 500
using namespace std;
struct node
{
    int x,y,dis;
    node(int x,int y,int dis):x(x),y(y),dis(dis) {}
    operator<(const node&a)const
    {
        return dis>a.dis;

    }


};
int dx[4]= {0,1,0,-1};
int dy[4]= {1,0,-1,0};
int vis[maxn][maxn];
char mp[maxn][maxn];
int sx,sy,ex,ey;
int n,m;
int bfs()
{
    priority_queueq;

    //while(!q.empty())
    //q.pop();
    memset(vis,0,sizeof(vis));
    q.push(node(sx,sy,0));
    vis[sx][sy]=1;
    while(!q.empty())
    {
        node u=q.top();
        q.pop();

        for(int i=0; i<4; i++)
        {
            int xx=u.x+dx[i];
            int yy=u.y+dy[i];
            int dis=u.dis+1;
            if(xx>=0&&xx=0&&yy

 

你可能感兴趣的:(bfs)