ZOJ 1649 Rescue

BFS求解监狱救援
本文介绍了一种使用BFS算法解决监狱救援问题的方法。通过分析ZOJ 1649题目,采用优先队列实现加权最短路径搜索,找到救援者到达目标位置所需的最少时间。

原题传送门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1649

 

Rescue

 


 

Time Limit: 2 Seconds      Memory Limit: 65536 KB

 


 

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

 

 

分析:这道题用BFS可以解决,不过这里有坑~!在BFS时候,不能用queue队列,而应该用priority_queue优先队列,因为这里每条路径的每个点权值不一样,有x的权值是2,而‘.’的权值只有1,如果用queue找的紧紧是最短路径,而不是加权路径最短的那条,所以用优先队列来处理。

 

 

//图的BFS ZOJ 1649
#include <stdio.h>
#include <queue>
#include <string.h>

using namespace std;
const int MAXN = 210;
char list[MAXN][MAXN];
bool isVisited[MAXN][MAXN];
bool flag;

struct Step{
    int x;
    int y;
    int step;
    bool operator < (const Step &rhs) const{
        return rhs.step < step;
    }
};
int m,n,mins;

priority_queue<Step> pos;
Step first,curStep;
main(){
    while(scanf("%d%d",&m,&n)!= EOF){
        mins = 100000000;
        while(!pos.empty()){
            pos.pop();
        }
        flag = false;
        memset(isVisited,false,sizeof(isVisited));
        for(int i=1;i<=m;i++){
            scanf("%s",list[i]+1);
        }
        for(int i=1;i<=m;i++){
            for(int j=1;j<=n;j++){
                if(list[i][j] == 'r'){
                    first.x = i;
                    first.y = j;
                    first.step = 0;
                }
            }
        }
        pos.push(first);
        isVisited[first.x][first.y] = true;
        while(!pos.empty()){
            curStep = pos.top();
            pos.pop();
            if(list[curStep.x][curStep.y] == 'a'){
               // printf("%d---%d",curStep.x,curStep.y);
                flag = true;
                mins = (mins>curStep.step)?curStep.step:mins;
            }
            if((list[curStep.x-1][curStep.y]=='.'||list[curStep.x-1][curStep.y]=='x'||
                list[curStep.x-1][curStep.y]=='a') && curStep.x-1>0 && !isVisited[curStep.x-1][curStep.y]){
                Step newStep;
                newStep.x = curStep.x-1;
                newStep.y = curStep.y;
                if(list[curStep.x-1][curStep.y]=='.'||list[curStep.x-1][curStep.y]=='a'){
                    newStep.step = curStep.step + 1;
                }else{
                    newStep.step = curStep.step + 2;
                }
                pos.push(newStep);
                isVisited[curStep.x-1][curStep.y] = true;
              //  printf("%d - %d\n",newStep.x,newStep.y);


            }
            if((list[curStep.x+1][curStep.y]=='.'||list[curStep.x+1][curStep.y]=='x'||
                list[curStep.x+1][curStep.y]=='a') && curStep.x+1<=m && !isVisited[curStep.x+1][curStep.y]){
                Step newStep;
                newStep.x = curStep.x+1;
                newStep.y = curStep.y;
                if(list[curStep.x+1][curStep.y]=='.'||list[curStep.x+1][curStep.y]=='a'){
                    newStep.step = curStep.step + 1;
                }else{
                    newStep.step = curStep.step + 2;
                }
                pos.push(newStep);
                isVisited[curStep.x+1][curStep.y] = true;
               // printf("%d - %d\n",newStep.x,newStep.y);
            }
            if((list[curStep.x][curStep.y-1]=='.'||list[curStep.x][curStep.y-1]=='x'||
                list[curStep.x][curStep.y-1]=='a') && curStep.y-1>0 && !isVisited[curStep.x][curStep.y-1]){
                Step newStep;
                newStep.x = curStep.x;
                newStep.y = curStep.y-1;

                if(list[curStep.x][curStep.y-1]=='.'||list[curStep.x][curStep.y-1]=='a'){
                    newStep.step = curStep.step + 1;
                }else{
                    newStep.step = curStep.step + 2;
                }
                pos.push(newStep);
                isVisited[curStep.x][curStep.y-1] = true;
               // printf("%d - %d\n",newStep.x,newStep.y);
            }
            if((list[curStep.x][curStep.y+1]=='.' ||list[curStep.x][curStep.y+1]=='x'||
                list[curStep.x][curStep.y+1]=='a' )&& curStep.y+1<=n && !isVisited[curStep.x][curStep.y+1]){
                Step newStep;
                newStep.x = curStep.x;
                newStep.y = curStep.y+1;
                if(list[curStep.x][curStep.y+1]=='.'||list[curStep.x][curStep.y+1]=='a'){
                    newStep.step = curStep.step + 1;
                }else{
                    newStep.step = curStep.step + 2;
                }
                pos.push(newStep);
                isVisited[curStep.x][curStep.y+1] = true;
               // printf("%d - %d\n",newStep.x,newStep.y);
            }
        }
        if(flag){
            printf("%d\n",mins);
        }else{
            printf("Poor ANGEL has to stay in the prison all his life.\n" );
        }
    }

}

 

 

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值