HDU-1010-Tempter of the Bone

本文介绍了一种迷宫逃脱算法,通过奇偶剪枝优化搜索过程。算法要求角色在限定时间内找到出口,同时避免重复路径。文章详细解释了算法原理,并提供了一个具体的实现案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

点击打开链接


Tempter of the Bone

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


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

题解:奇偶剪枝,如果走偶数步要求的时间是奇数,或者走奇数步要求的时间是偶数,都明显不可行。

所谓奇偶剪纸指的是,剩余时间为奇数时,剩余步数也要为奇数,剩余时间为偶数时,剩余步数也要为偶数,这样才能满足条件。

达到这两个条件要满足,剩余时间-剩余步数==偶数,如果为奇数那么必定不满足条件,就“剪去”,不用考虑了。

#include<stdio.h>  
#include<string.h>  
#include<math.h>  
using namespace std;  
int n,m,t,ex,ey,sx,sy;  
int flag,wall,res,rx,ry;  
int step;  
char a[80][80];  
int fx[]={1,-1,0,0},fy[]={0,0,1,-1};  
void dfs(int x,int y,int step)  
{  
    if(flag)  
    return ;  
    if(x==ex&&y==ey&&step==t)   //条件刚好,到终点时间刚好用完   
    {  
        flag=1;  
        return ;  
    }  
    rx=abs(x-ex);       //从当前位置到终点需要横向一定的步数  
    ry=abs(y-ey);	//从当前位置到终点需要纵向移动的步数  
    res=t-step-rx-ry;	//剩余时间==总时间-用去的时间-剩余步数  
    if(res<0||res%2!=0)  //看剩下的时间能能否到达终点,tem&1则是判断其是否偶数,根据LCY的奇  
    return ;            //偶性剪枝可得tem必须是偶数,是奇数则不行  
      
      
  
    for(int i=0;i<4;i++)  
    {  
        int xx=x+fx[i],yy=y+fy[i];  
        if(xx>=0&&xx<m&&yy>=0&&yy<n&&a[xx][yy]!='X')  
        {  
            a[xx][yy]='X';      //走过的地方变为墙   
            dfs(xx,yy,step+1);  
            a[xx][yy]='.';      //迷宫还原,以便下次广搜  
        }  
    }  
 }   
int main()  
{  
    while(~scanf("%d%d%d",&m,&n,&t))  
    {  
        int i,j;  
        wall=0;  
        flag=0;  
        step=0;  
        if(m==0&&n==0&&t==0)  
        break;  
        for(i=0;i<m;i++)  
            scanf("%s",a[i]);  
        for(i=0;i<m;i++)  
            for(j=0;j<n;j++)  
            {  
                if(a[i][j]=='S')    //找到起点   
                sx=i,sy=j;  
                if(a[i][j]=='D')    //找到终点   
                ex=i,ey=j;  
                if(a[i][j]=='X')    //记录墙的总数   
                wall++;   
            }  
        if(m*n-wall<=t)  //临街wall+t+1==m*n,所以正常情况下m*n-wall应该>t   
        {  
            printf("NO\n");  
            continue;  
        }  
        a[sx][sy]='X';  //出发点是不可能再走的了,变为墙    
        dfs(sx,sy,step);      
        if(flag)  
        printf("YES\n");  
        else printf("NO\n");  
    }  
}  



资源下载链接为: https://pan.quark.cn/s/22ca96b7bd39 在当今的软件开发领域,自动化构建与发布是提升开发效率和项目质量的关键环节。Jenkins Pipeline作为一种强大的自动化工具,能够有效助力Java项目的快速构建、测试及部署。本文将详细介绍如何利用Jenkins Pipeline实现Java项目的自动化构建与发布。 Jenkins Pipeline简介 Jenkins Pipeline是运行在Jenkins上的一套工作流框架,它将原本分散在单个或多个节点上独立运行的任务串联起来,实现复杂流程的编排与可视化。它是Jenkins 2.X的核心特性之一,推动了Jenkins从持续集成(CI)向持续交付(CD)及DevOps的转变。 创建Pipeline项目 要使用Jenkins Pipeline自动化构建发布Java项目,首先需要创建Pipeline项目。具体步骤如下: 登录Jenkins,点击“新建项”,选择“Pipeline”。 输入项目名称和描述,点击“确定”。 在Pipeline脚本中定义项目字典、发版脚本和预发布脚本。 编写Pipeline脚本 Pipeline脚本是Jenkins Pipeline的核心,用于定义自动化构建和发布的流程。以下是一个简单的Pipeline脚本示例: 在上述脚本中,定义了四个阶段:Checkout、Build、Push package和Deploy/Rollback。每个阶段都可以根据实际需求进行配置和调整。 通过Jenkins Pipeline自动化构建发布Java项目,可以显著提升开发效率和项目质量。借助Pipeline,我们能够轻松实现自动化构建、测试和部署,从而提高项目的整体质量和可靠性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值