HDOJ 1242 Rescue

本文介绍了一种使用优先队列实现的广度优先搜索算法解决监狱救援问题的方法,旨在找到从起点到终点的最短路径,考虑到不同地形的时间成本。

Rescue

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

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
题意:
从a处(起点)走到r处(终点)
路过’ . ‘算一个单位时间
路过’ x '算两个单位时间
使用深搜需要搜出所有解,会超时
使用广搜可能不是最优解
例如
5 5
. . . . r
. . . . x
. . . . x
. . . . x
axxxx
使用广搜+优先队列,每次先走 时间 最少的哪一个点,能保证第一次走到终点是为最优解

#include <iostream>
#include <queue>
#include <cstdio>
#include <algorithm>
#include <cstring>
const int maxsize = 1000;
using namespace std;
struct point {
int x,y;
int step;
bool operator< (const point & a) const
    {
        return step>a.step;
    }
};
int flag[maxsize][maxsize];
char mapp[maxsize][maxsize];
int a[]= {0,0,1,-1};
int b[]= {1,-1,0,0};
void bfs(point aa);
int n,m;
int sx,sy;
int endx,endy;
int ans;
priority_queue<point> q;
int main()
{
    while(cin>>n>>m)
    {
        if(n==0)break;
        ans=0;
        memset(flag,0,sizeof(flag));
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                cin>>mapp[i][j];
                if(mapp[i][j]=='a'){
                    sx=i;sy=j;
                }
                if(mapp[i][j]=='r'){
                    endx=i;endy=j;
                }
            }
        }
        flag[sx][sy]=1;
        point z={sx,sy,0};
        bfs(z);
        while(q.empty()==0)q.pop();
        if(ans==0)cout<<"Poor ANGEL has to stay in the prison all his life.\n";//走不到
        else cout<<ans<<endl;
    }
    return 0;
}
void bfs( point aa ){
    q.push(aa);//a点入队
    while(q.empty()==0)//队列不为空
    {
        point tmp=q.top();//取出
        q.pop();
        if(tmp.x==endx&&tmp.y==endy)
        {
            ans=tmp.step;
            return ;
        }
        for(int k=0;k<4;k++)
        {
            int x=tmp.x+a[k];
            int y=tmp.y+b[k];
            if(x>=0&&x<n&&y>=0&&y<m&&flag[x][y]==0&&(mapp[x][y]=='.'||mapp[x][y]=='a'||mapp[x][y]=='r'))
            {
                flag[x][y]=1;
                point t1={x,y,tmp.step+1};
                q.push(t1);

            }
            else if(x>=0&&x<n&&y>=0&&y<m&&flag[x][y]==0&&mapp[x][y]=='x')
            {
                flag[x][y]=1;
                point t2={x,y,tmp.step+2};
                q.push(t2);
            }
        }
    }

}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值