poj 2251 Dungeon Master

探讨了在一个三维迷宫中寻找从起点到终点最短路径的问题,利用广度优先搜索算法实现,并提供了完整的C++代码示例。
Dungeon Master
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 26930 Accepted: 10552

Description

You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides. 

Is an escape possible? If yes, how long will it take? 

Input

The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size). 
L is the number of levels making up the dungeon. 
R and C are the number of rows and columns making up the plan of each level. 
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are represented by a '.'. Your starting position is indicated by 'S' and the exit by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for L, R and C.

Output

Each maze generates one line of output. If it is possible to reach the exit, print a line of the form 
Escaped in x minute(s).

where x is replaced by the shortest time it takes to escape. 
If it is not possible to escape, print the line 
Trapped!

Sample Input

3 4 5
S....
.###.
.##..
###.#

#####
#####
##.##
##...

#####
#####
#.###
####E

1 3 3
S##
#E#
###

0 0 0

Sample Output

Escaped in 11 minute(s).
Trapped!

Source

/*

题目大意: 
 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径

移动方向可以是上,下,左,右,前,后,六个方向

每移动一次就耗费一分钟,要求输出最快的走出时间。
不同L层的地图,相同RC坐标处是连通的

*/

最短路就用广搜

#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <algorithm>
#include <queue>
using namespace std;

struct node
{
    int x;
    int y;
    int z;
    int step;
};
char Map[40][40][40];
int x1, y1, z1;
int x2, y2, z2;
int x,y,z;
queue<node> q;
int vis[40][40][40];
int dx[6] = {1, -1, 0, 0, 0, 0};
int dy[6] = {0, 0, 1, -1, 0, 0};
int dz[6] = {0, 0, 0, 0, 1, -1};

int bfs(int xx, int yy, int zz)
{
    int i;
    while ( !q.empty() )
    {
        q.pop();
    }
    node temp;
    temp.x = xx;
    temp.y = yy;
    temp.z = zz;
    temp.step = 0;
    q.push(temp);
    while ( !q.empty() )
    {
        node flag;
        node flag1;
        flag = q.front();
        q.pop();
        for ( i = 0;i < 6; i++ )
        {
            if (  (flag.x+dx[i] >= 0&&flag.x+dx[i] < x) && (flag.y+dy[i] >= 0&&flag.y+dy[i] < y) && (flag.z+dz[i] >= 0&&
                  flag.z+dz[i] < z) && (vis[flag.x+dx[i]][flag.y+dy[i]][flag.z+dz[i]] == 0) && (Map[flag.x+dx[i]][flag.y+dy[i]][flag.z+dz[i]] != '#') )
            {
                flag1.x = flag.x+dx[i];
                flag1.y = flag.y+dy[i];
                flag1.z = flag.z+dz[i];
                vis[flag1.x][flag1.y][flag1.z] = 1;
                flag1.step = flag.step+1;
                if (flag1.x == x2&&flag1.y == y2&&flag1.z == z2)
                {
                    return flag1.step;
                }
                q.push(flag1);
            }
        }
    }
    return -1;
}

int main()
{
    int i, j, k;
    while ( ~scanf ( "%d %d %d", &x, &y, &z ) && (x != 0&&y != 0&&z != 0) )
    {
        memset(vis, 0, sizeof(vis));
        for ( i = 0;i < x; i++ )
        {
            for ( j = 0;j < y; j++ )
            {
                scanf ( "%s", Map[i][j] );
                for ( k = 0;k < z ; k++ )
                {
                    if ( Map[i][j][k] == 'S' )
                    {
                        x1 = i;
                        y1 = j;
                        z1 = k;
                    }
                    if ( Map[i][j][k] == 'E' )
                    {
                        x2 = i;
                        y2 = j;
                        z2 = k;
                    }
                }
            }
        }
        vis[x1][y1][z1] = 1;
        int sum = bfs(x1, y1, z1);
        if ( sum == -1 )
            printf ( "Trapped!\n" );
        else
            printf ( "Escaped in %d minute(s).\n", sum );
    }
}

代码菜鸟,如有错误,请多包涵!!!
如果有帮助记得鼓励我一下,谢谢!!!
内容概要:本文系统介绍了算术优化算法(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、付费专栏及课程。

余额充值