Tempter of the Bone 回溯法 还有奇偶剪枝

简介: Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 78529    Accepted...

Tempter of the Bone

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


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
 

Author
ZHANG, Zheng
 

Source
 

Recommend
JGShining   |   We have carefully selected several similar problems for you:   1016  1241  1072  1312  1026 
 


这道题啊,折磨了我好久,开始觉得,哦,BFS做就好了,但是范例能过,而实际上却不是合适办法,所以如果对于这道题还是有疑问的同学,只要看看这样一副图就好了。

2 4 7
S..D
....
所以最后参考大神们的代码,解决了本题中的关键问题。
这幅图的答案应该是yes,但是BFS的特点,就是只会保持同一层的而不能像DFS一样将整条路都递归出来,这样的好处是因为本题不能回头,所以每一条路的寻找都不能影响到其他的路径的寻找,每一次标记然后递归后要把标记去掉,所以这道题用回溯法。而且还有奇偶剪枝。

对于奇偶剪枝因为如果对于任何一个位置所在,还需要走的距离是奇数而剩下的时间是偶数,那么可以直接不考虑这条路了,主人公活不了的。直接return掉好了。

所以需要这样来避免时间上被卡住。

代码君

#include<cstdio>
#include<cmath>
#include<cstring>

using namespace std;
int   vid[9][9];
char  way[9][9];
int m,n,t;
bool flag = false;
struct Steps{
    int x,y;
};
Steps   walk,en;
const int mov[4][2] = {0,1,0,-1,1,0,-1,0};
void backtrack(int bx,int by,int ust){
    int need;
    if(flag)
        return;
    if(ust == t){
        if(bx == en.x && by == en.y){
            flag = true;
            return;
        }
    }
    need = fabs(bx - en.x)+fabs(by - en.y);
    int temp = t - ust - need;
    if(temp < 0||temp&1){
        return;
    }
    for(int i = 0;i < 4;i++){
        int tx,ty;
        tx = bx + mov[i][0];
        ty = by + mov[i][1];
        if(tx >= 0 && ty >= 0 && tx < n && ty < m && (way[ty][tx] == '.' || way[ty][tx] == 'D') && !vid[ty][tx]){
            vid[ty][tx] = 1;
            backtrack(tx,ty,ust+1);
            vid[ty][tx] = 0;
        }
    }
}
int main(){
    while(~scanf("%d %d %d",&m,&n,&t) && m){
        memset(way,'X',sizeof(way));
        flag = false;
        for(int i = 0;i < m;i++){
            for(int j = 0;j < n;j++){
                scanf(" %c",&way[i][j]);
                if(way[i][j] == 'S'){
                    walk.x = j;
                    walk.y = i;
                }
                if(way[i][j] == 'D'){
                    en.x = j;
                    en.y = i;
                }
            }
        }
        memset(vid,0,sizeof(vid));
        vid[walk.y][walk.x] = 1;
        backtrack(walk.x,walk.y,0);
        if(flag)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return  0;
}


相关文章
|
应用服务中间件 nginx 弹性计算
阿里云SLB上http强制跳转到https问题处理
在阿里云SLB上使用https访问设置时,出现443端口健康检测始终失败,逐步排查最终解决了该问题
24472 0
|
云安全 安全 网络安全
|
6天前
|
JavaScript 安全 前端开发
Nodejs Brotli decoder 代码执行漏洞 (CVE-2026-111497)10629
Node.js发布安全更新修复Brotli解码器高危RCE漏洞(CVE-2026-111497),建议受影响版本用户立即升级。
1977 1
|
12天前
|
人工智能 API 开发工具
DeepSeek Harness 神插件:白嫖 13 家大厂模型
`dsh-our-free-model` 是 DeepSeek Harness 神级插件:零配置、零API Key、零账号,聚合13家大厂免费模型(含DeepSeek V4.1 Flash、Kimi K3等),支持本地网关、用量看板与自动降级,真正实现“白嫖即用”。
2083 41
|
9天前
|
Web App开发 人工智能 前端开发
2026 年 AI 浏览器怎么选?Ego Lite、Tabbit、Chrome DevTools MCP 深度对比,开发者到底该用哪个?
2026年AI浏览器选型指南:Ego Lite专注多Agent协同(Mac优先),Tabbit国产首选,原生支持Windows与Codex控制;Chrome DevTools MCP非浏览器而是开发者利器,深度集成Console/Network/Performance调试能力,让AI真正“看懂并验证”前端代码。
1377 3
|
13天前
|
JSON 程序员 数据格式
最新版Notepad++下载、安装、配置保姆级教程(附官网安装包)
Notepad++ 是一款免费开源、轻量快速的 Windows 文本编辑器,支持多标签、语法高亮、代码折叠与宏录制,体积小、启动快、资源占用低,兼容 x64/x32/ARM 架构,是程序员和文字工作者的高效利器。
2573 0
|
6天前
|
安全 应用服务中间件 Go
PHP2Shell 漏洞拆解:WordPress 与 nginx 为何中招
PHP2Shell 双 RCE 已遭在野利用:WordPress、Drupal 全在射程内,nginx 其实另有洞。拆成因与修复顺序
PHP2Shell 漏洞拆解:WordPress 与 nginx 为何中招
|
14天前
|
安全 Linux 芯片
【万字长文】Ollama下载、安装、配置、使用保姆级教程(2026最新图解)
Ollama 是一款免费开源的本地大模型运行工具,支持 DeepSeek、Qwen 等主流模型,一条命令即可下载运行。2026 最新版 v0.35.1 官方安装包(1.47GB)已提供国内直连下载,纯本地、离线可用、无广告、无隐私泄露风险。
1286 5
|
7天前
|
开发者 索引
DeepSeekHarness大肥鱼自带插件市场了?加个地址就能用
Open Plugin Market 是一个开放插件索引平台,收录800+优质DSH插件,支持分类浏览、智能推荐与随机发现。无需安装市场,粘贴索引地址即可一键接入,网页版直访 dshopen.org,助力小开发者被看见。(239字)
1436 1
DeepSeekHarness大肥鱼自带插件市场了?加个地址就能用

热门文章

最新文章