hdu 1010 Tempter of the Bone(dfs剪枝+回溯)

本文介绍了一个迷宫逃脱问题,通过深度优先搜索(DFS)解决小狗如何在限定时间内找到出口的问题。考虑了迷宫布局、时间限制及地面消失等条件,采用剪枝策略优化搜索过程。

Tempter of the Bone

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


Problem Description
The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone was a trap, and he tried desperately to get out of this maze.

The maze was a rectangle with sizes N by M. There was a door in the maze. At the beginning, the door was closed and it would open at the T-th second for a short period of time (less than 1 second). Therefore the doggie had to arrive at the door on exactly the T-th second. In every second, he could move one block to one of the upper, lower, left and right neighboring blocks. Once he entered a block, the ground of this block would start to sink and disappear in the next second. He could not stay at one block for more than one second, nor could he move into a visited block. Can the poor doggie survive? Please help him.
 

Input
The input consists of multiple test cases. The first line of each test case contains three integers N, M, and T (1 < N, M < 7; 0 < T < 50), which denote the sizes of the maze and the time at which the door will open, respectively. The next N lines give the maze layout, with each line containing M characters. A character is one of the following:

'X': a block of wall, which the doggie cannot enter; 
'S': the start point of the doggie; 
'D': the Door; or
'.': an empty block.

The input is terminated with three 0's. This test case is not to be processed.
 

Output
For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.
 

Sample Input
4 4 5 S.X. ..X. ..XD .... 3 4 5 S.X. ..X. ...D 0 0 0
 

Sample Output
NO YES
 

题意:一只小狗在迷宫里闻到了一块骨头的香味,要刚好T时刻到达才能得到,走过的格子会消失。
dfs剪枝 1.从a出发到b点的方格问题中a到b的各条路径与a到b的最短路径同奇偶
        2.格子数小于时间T就不可能到达
dfs回溯
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
using namespace std;
int n,m,t,ans,q,p;
int dir[][2]={1,0,-1,0,0,1,0,-1};//方向
char map[10][10];
void dfs(int x ,int y,int T)
{
    if(x==q&&y==p&&T==t)
    {
        ans=1;
        return ;
    }
    int s=t-fabs(x-q)-fabs(y-p)-T;//剪枝1
    if(s<0||s%2)
        return ;
    for(int i=0;i<4;i++)
    {
        int a=x+dir[i][0];
        int b=y+dir[i][1];
        if(a<1||b<1||a>n||b>m||map[a][b]=='X')
            continue;
        map[a][b]='X';
        dfs(a,b,T+1);
        if(ans)
            return ;
        map[a][b]='.';//回溯
    }
    return ;
}
int main()
{
    while(cin>>n>>m>>t,n+m+t)
    {
        int x,y,count=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                cin>>map[i][j];
                if(map[i][j]=='S')
                {
                    x=i;
                    y=j;
                }
                if(map[i][j]=='D')
                {
                    q=i;
                    p=j;
                }
                if(map[i][j]=='X')
                    count++;
            }
        }
        if(n*m-count<=t)//剪枝2
        {
            cout<<"NO"<<endl;
            continue;
        }
        ans=0;
        map[x][y]='X';
        dfs(x,y,0);
        if(ans==1)
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;
    }
    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、付费专栏及课程。

余额充值