HDU - 1242

题目链接:https://cn.vjudge.net/contest/292780#problem/E
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1242
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

题意:
天使被囚禁在N*M矩阵的监狱,监狱里使的朋友想要拯救天使。
他们的任务是:接近天使。“接近天使”是为了达到天使留下的位置。当网格中有一名后卫时,我们必须杀死他(或她?)才能进入网格。我们假设我们向上,向下,向右,向左移动需要1个单位时间,并且杀死一名守卫也需要1个单位时间。我们足够强大,可以杀死所有的守卫。
“."代表道路,“a”代表Angel,“r”代表Angel的每个朋友,"x"代表守卫。

解题思路:
首先,我们马上就会想到bfs,确实搜索可以找到,但这题的一个条件选择的是需要消耗最少时间的那条路,所以,我们需要用优先队列,这样就可以确保输出的是最短时间。

如果不知什么是优先队列,可以看看我的另一个博客:我是博客

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<vector>
#include<queue>
using namespace std;
const int maxn = 200 + 10;
char mp[maxn][maxn];
int vis[maxn][maxn];
int n, m;
struct node//优先队列
{
    int x, y, step;
    friend bool operator<(node a, node b)
    {
    return a.step > b.step;//步数
    }
}; 
int d[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
void BFS(int x1, int y1, int x2, int y2)
{
    memset(vis, 0, sizeof(vis));
    priority_queue<node> que;//优先队列
    node e1, e2;
    e1.x = x1, e1.y = y1, e1.step = 0; // 起点
    que.push(e1);
    vis[x1][y1] = 1; //标记已经走过
    int ans = -1;
    while (!que.empty())
    {
        e1 = que.top();
        que.pop();
        if (e1.x == x2 && e1.y == y2)
        {
            ans = e1.step;
            break;
        }
        for (int i = 0; i < 4; i++)
        {
            e2.x = e1.x + d[i][0];
            e2.y = e1.y + d[i][1];
            if (e2.x < 0 || e2.x >= n || e2.y < 0 || e2.y >= m||vis[e2.x][e2.y]||mp[e2.x][e2.y] == '#') continue;
            if (mp[e2.x][e2.y] == 'x') e2.step = e1.step + 2;//杀要守卫多一秒
            else e2.step = e1.step + 1;
            que.push(e2);
            vis[e2.x][e2.y] = 1;
        }
    }
    if (ans==-1) 
    cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
    else
    printf("%d\n",ans);
}
int main()
{
    while(~scanf("%d%d",&n,&m)){
    for(int i=0;i<n;i++)
    {
        getchar();
        cin>>mp[i];
    }
    int q1,w1,q2,w2;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(mp[i][j]=='r')
            {
                q1=i;
                w1=j;
            }
            if(mp[i][j]=='a')
            {
                q2=i;
                w2=j;
            }
        }
    }
        BFS(q2, w2, q1, w1);
    }
    return 0;
}
内容概要:本文系统介绍了算术优化算法(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、付费专栏及课程。

余额充值