Sicily 1722. Hero

超级马里奥救援公主算法题
本题涉及超级马里奥救援被困公主的情景,玩家需帮助马里奥利用地图上的弹簧机关快速到达公主所在位置。题目要求计算最少所需时间,并考虑了地图边界、弹簧连续使用等复杂情况。

1722. Hero

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

The beautiful princess is caught by the demon again. Super Mario, her lover, has to take the responsibility to be the hero. The princess is incarcerated in a horrific castle which is smelly and full of monsters. She is very scared. Super Mario is very worried about the princess and wants to see the princess as soon as possible. Though he runs fast, he still wants to know how fast it will take to reach the place where the princess is.
The castle is a N*M matrix. Initially Mario and princess are located in different positions. Mario can move up, down, left, right, one step per second. Specially, there are some springs distributed in the castle (Don’t ask why, maybe the demon likes to play spring.^_^). These springs are supernatural and may be used as a tool to help Mario if used properly. Each of the spring has an attribute ?? spring power which is an integer number actually. When Mario enters a grid where there is a spring whose spring power is k, he will be sprung k grids following the direction he enters the grid in no time.


For example, supposed Mario is in (2,8), and there is a spring in grid (2,7) with 5 spring power. If Mario goes left one step, he will be sprung 5 grids left. So the final position of Mario is (2,2) and the time from (2,8) to (2,2) for Mario is just one second. 
Note that if Mario is sprung outside the matrix, he will be stopped by the wall of the castle and drop on the grid beside the wall. For example, supposed Mario is in (2,8), and there is a spring in grid (2,7) with 10 spring power. If Mario goes left one step, he will just sprung 6 grids left, then stop and drop in grid (2,1).
Moreover, if the position where Mario will land has a spring, he can be sprung again. That means Mario can be sprung consecutively until he lands on a grid without spring. And only the landing position may affect Mario’s action, all grids that Mario passes by when he is sprung have nothing to do with him. For example, supposed Mario is in (2,8) and there are two grid have springs – (2,7) and (2,5), both are 5 spring power. If Mario steps left, he will be sprung to position (2,2). So the spring in (2,5) does not work on him.
It is your task to tell Mario the minimum time to reach the princess. 

Input

Input contains several test cases and is terminated by EOF.
Each case begins with one line containing two integers, n and m (3 <= n,m <= 100), which are the row number and column number of the castle. Then follows a non-negative integer number k, indicating the number of springs. Then follows k lines, each line has three positive integers - x,y, and p, x,y is the coordinate of the spring (2<= x <=n-1,2 <= y <= m-1) and p is the spring power described above. These are followed by 2 lines lastly, the first line contains two integers, which represent the coordinate of Mario, and the other line is the coordinate of the princess.
You can assume all of the cases are legal. The initial positions of Mario, princess, and springs are distinct. All of the x coordinates are in the range of [1, n] and all of the y coordinates are in the range of [1, m]. And no spring is next to the wall of the castle.

Output

For each test case, just output one line presenting the minimum time Mario will spend. If Mario can not save the princess (Oh, my god*_* but it may happen), just print “Impossible“ in one line(without quotation marks) .

Sample Input

10 10
0
2 2
7 8
10 10
1
2 7 5
2 8
1 1
10 10
1
2 7 10
2 8 
1 1
10 10
2
2 7 5
2 5 5
2 8
1 1
10 10
4
7 9 2
7 7 2
6 8 2
8 8 2
2 2 
7 8

Sample Output

11
3
2
3

Impossible

// Problem#: 1722
// Submission#: 3585370
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <stdio.h>
#include <queue>
#include <string.h>
using namespace std;

bool vis[105][105][4];
int G[105][105];
int h, w;
int si, sj, ei, ej;
int dir[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};

struct step {
    int i, j, s;
    step(int ii = 0, int jj = 0, int ss = 0) {
        i = ii;
        j = jj;
        s = ss;
    }
};

inline bool isValid(int i, int j, int dir) {
    if (!(1 <= i && i <= h && 1 <= j && j <= w)) return false;
    if (G[i][j] == 0 && vis[i][j][0]) return false;
    if (G[i][j] && vis[i][j][dir]) return false;
    return true;
}

void BFS() {
    queue<step> q;
    memset(vis, false, sizeof(vis));
    vis[si][sj][0] = true;
    q.push(step(si, sj, 0));
    int ans = 88888888;
    while (!q.empty()) {
        step t = q.front();
        //printf("---%d %d---\n", t.i, t.j);
        q.pop();
        if (t.i == ei && t.j == ej) {
            if (ans > t.s) ans = t.s;
        }
        for (int i = 0; i < 4; i++) {
            int ni = t.i + dir[i][0];
            int nj = t.j + dir[i][1];
            if (isValid(ni, nj, i)) {
                if (G[ni][nj] == 0) {
                    q.push(step(ni, nj, t.s + 1));
                    vis[ni][nj][0] = true;
                } else {
                    while (G[ni][nj] && !vis[ni][nj][i]) {
                        vis[ni][nj][i] = true;
                        if (i == 0) {
                            nj += G[ni][nj];
                            if (nj >= w) nj = w;
                        } else if (i == 1) {
                            ni += G[ni][nj];
                            if (ni >= h) ni = h;
                        } else if (i == 2) {
                            nj -= G[ni][nj];
                            if (nj <= 1) nj = 1;
                        } else {
                            ni -= G[ni][nj];
                            if (ni <= 1) ni = 1;
                        }
                    }
                    q.push(step(ni, nj, t.s + 1));
                    vis[ni][nj][0] = true;
                }
            }
        }
    }
    if (ans == 88888888) printf("Impossible\n");
    else printf("%d\n", ans);
}


int main() {
    while (scanf("%d%d", &h, &w) != EOF) {
        memset(G, 0, sizeof(G));
        int k;
        scanf("%d", &k);
        while (k--) {
            int i, j;
            scanf("%d%d", &i, &j);
            scanf("%d", &G[i][j]);
        }
        scanf("%d%d%d%d", &si, &sj, &ei, &ej);
        BFS();
    }
    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、付费专栏及课程。

余额充值