hdu 1242优先队列

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." 

扫描二维码关注公众号,回复: 4312998 查看本文章

 

Sample Input

 

7 8 #.#####. #.a#..r. #..#x... ..#..#.# #...##.. .#...... ........

 

Sample Output

 

13

 

Author

CHEN, Xue

 

Source

ZOJ Monthly, October 2003

 

Recommend

Eddy

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

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

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
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;
queue<node>q;
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<dis[xx][yy]))
            {
                dis[xx][yy]=dist;
                q.push(node(xx,yy,dist));

            }
        }
    }
    return dis[ex][ey];

}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {while(!q.empty())
    q.pop();
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
            scanf(" %c",&mp[i][j]);
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
            {if(mp[i][j]=='r')
            {q.push(node(i,j,0));
            dis[i][j]=0;
            mp[i][j]='.';
    }
     else if(mp[i][j]=='a')
        {
            ex=i;
            ey=j;
           // mp[i][j]='.';
        }
}
int ans=bfs();
if(ans==-1)
    printf("Poor ANGEL has to stay in the prison all his life.\n" );
else
    printf("%d\n",ans);
    }
    return 0;
}

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

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
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;
queue<node>q;
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<dis[xx][yy]))
            {
                dis[xx][yy]=dist;
                q.push(node(xx,yy,dist));

            }
        }
    }
    return dis[ex][ey];

}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {while(!q.empty())
    q.pop();
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
            scanf(" %c",&mp[i][j]);
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
            {if(mp[i][j]=='r')
            {q.push(node(i,j,0));
            dis[i][j]=0;
            mp[i][j]='.';
    }
     else if(mp[i][j]=='a')
        {
            ex=i;
            ey=j;
           // mp[i][j]='.';
        }
}
int ans=bfs();
if(ans==-1)
    printf("Poor ANGEL has to stay in the prison all his life.\n" );
else
    printf("%d\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sdauguanweihong/article/details/84670759