hdoj1010 Tempter of the Bone(DFS迷宫)在规定时间内刚好到达

2023-10-09 11:48

本文主要是介绍hdoj1010 Tempter of the Bone(DFS迷宫)在规定时间内刚好到达,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目链接:

hdoj1010

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

题意:给出一个n*m的迷宫和时间t,图中X表示墙不能走过,S表示起点,D表示门,.表示可以走的路。每走一步需要1s,从S到D的时间不能大于t,也不能小于t,只能刚好等于t,不能回过头走重复的路,因为走完的路在1s之内就会塌陷,如果能够在规定时间刚好到达出口,则输出YES,否则就输出NO。
思路:先用字符二维数组a存迷宫,想好终止条件count > t和此时访问的刚好是D并且走过的时间刚好等于规定的时间,用一个success来标记有没有成功找到出口,如果在此时刚好访问到D并且走过的时间刚好等于规定的时间就标记success=true。在当前经历上为S或者.的时候,就将flag[sx][sy]标记为1,然后分别预测下一步是什么如果不超过数组范围并且没有访问过的话,就递归再深搜,这些条件写好后再回溯标记flag[sx][sy]为0,标记为未访问过,因为该点可以有多条路径经过,其实就是在枚举路径。

#include<iostream>
#include<cstdio>
#include<stack> 
#include<queue>
#include<vector>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<set>
#include<map>using namespace std;char a[10][10];
int n,m,t;
int x,y;
int flag[10][10] = {0};
bool success;void dfs(int sx,int sy,int count) {if(count > t) {return;} else if(a[sx][sy] == 'D' && count == t) {success = true;return;} else if(a[sx][sy] == 'S' || a[sx][sy] == '.') {flag[sx][sy] = 1;
//		cout << sx << sy << endl; if(sy + 1 < m && flag[sx][sy + 1] == 0) dfs(sx,sy + 1,count + 1); //向右走一步 if(sy - 1 >= 0 && flag[sx][sy - 1] == 0) dfs(sx,sy - 1,count + 1); //向左走一步 if(sx + 1 < n && flag[sx + 1][sy] == 0) dfs(sx + 1,sy,count + 1); //向下走一步 if(sx - 1 >= 0 && flag[sx - 1][sy] == 0) dfs(sx - 1,sy,count + 1); //向上走一步 flag[sx][sy] = 0;}
}int main() {while(~scanf("%d%d%d",&n,&m,&t) && (m + n + t)) {for(int i = 0;i < n;i++) {for(int j = 0;j < m;j++) {cin >> a[i][j];}}success = false;for(int i = 0;i < n;i++) {for(int j = 0;j < m;j++) {flag[i][j] = 0;if(a[i][j] == 'S') {x = i;y = j;			}}}dfs(x,y,0);if(success) {printf("YES\n");} else {printf("NO\n");}}return 0;
}

这篇关于hdoj1010 Tempter of the Bone(DFS迷宫)在规定时间内刚好到达的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/172627

相关文章

C++ 函数 strftime 和时间格式示例详解

《C++函数strftime和时间格式示例详解》strftime是C/C++标准库中用于格式化日期和时间的函数,定义在ctime头文件中,它将tm结构体中的时间信息转换为指定格式的字符串,是处理... 目录C++ 函数 strftipythonme 详解一、函数原型二、功能描述三、格式字符串说明四、返回值五

从基础到进阶详解Pandas时间数据处理指南

《从基础到进阶详解Pandas时间数据处理指南》Pandas构建了完整的时间数据处理生态,核心由四个基础类构成,Timestamp,DatetimeIndex,Period和Timedelta,下面我... 目录1. 时间数据类型与基础操作1.1 核心时间对象体系1.2 时间数据生成技巧2. 时间索引与数据

利用Python实现时间序列动量策略

《利用Python实现时间序列动量策略》时间序列动量策略作为量化交易领域中最为持久且被深入研究的策略类型之一,其核心理念相对简明:对于显示上升趋势的资产建立多头头寸,对于呈现下降趋势的资产建立空头头寸... 目录引言传统策略面临的风险管理挑战波动率调整机制:实现风险标准化策略实施的技术细节波动率调整的战略价

Python日期和时间完全指南与实战

《Python日期和时间完全指南与实战》在软件开发领域,‌日期时间处理‌是贯穿系统设计全生命周期的重要基础能力,本文将深入解析Python日期时间的‌七大核心模块‌,通过‌企业级代码案例‌揭示最佳实践... 目录一、背景与核心价值二、核心模块详解与实战2.1 datetime模块四剑客2.2 时区处理黄金法

macOS Sequoia 15.5 发布: 改进邮件和屏幕使用时间功能

《macOSSequoia15.5发布:改进邮件和屏幕使用时间功能》经过常规Beta测试后,新的macOSSequoia15.5现已公开发布,但重要的新功能将被保留到WWDC和... MACOS Sequoia 15.5 正式发布!本次更新为 Mac 用户带来了一系列功能强化、错误修复和安全性提升,进一步增

Pandas进行周期与时间戳转换的方法

《Pandas进行周期与时间戳转换的方法》本教程将深入讲解如何在pandas中使用to_period()和to_timestamp()方法,完成时间戳与周期之间的转换,并结合实际应用场景展示这些方法的... 目录to_period() 时间戳转周期基本操作应用示例to_timestamp() 周期转时间戳基

JavaScript时间戳与时间的转化常用方法

《JavaScript时间戳与时间的转化常用方法》在JavaScript中,时间戳(Timestamp)通常指Unix时间戳,即从1970年1月1日00:00:00UTC到某个时间点经过的毫秒数,下面... 目录1. 获取当前时间戳2. 时间戳 → 时间对象3. 时间戳php → 格式化字符串4. 时间字符

Java controller接口出入参时间序列化转换操作方法(两种)

《Javacontroller接口出入参时间序列化转换操作方法(两种)》:本文主要介绍Javacontroller接口出入参时间序列化转换操作方法,本文给大家列举两种简单方法,感兴趣的朋友一起看... 目录方式一、使用注解方式二、统一配置场景:在controller编写的接口,在前后端交互过程中一般都会涉及

python处理带有时区的日期和时间数据

《python处理带有时区的日期和时间数据》这篇文章主要为大家详细介绍了如何在Python中使用pytz库处理时区信息,包括获取当前UTC时间,转换为特定时区等,有需要的小伙伴可以参考一下... 目录时区基本信息python datetime使用timezonepandas处理时区数据知识延展时区基本信息

Python的time模块一些常用功能(各种与时间相关的函数)

《Python的time模块一些常用功能(各种与时间相关的函数)》Python的time模块提供了各种与时间相关的函数,包括获取当前时间、处理时间间隔、执行时间测量等,:本文主要介绍Python的... 目录1. 获取当前时间2. 时间格式化3. 延时执行4. 时间戳运算5. 计算代码执行时间6. 转换为指