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

相关文章

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. 转换为指

Java中字符串转时间与时间转字符串的操作详解

《Java中字符串转时间与时间转字符串的操作详解》Java的java.time包提供了强大的日期和时间处理功能,通过DateTimeFormatter可以轻松地在日期时间对象和字符串之间进行转换,下面... 目录一、字符串转时间(一)使用预定义格式(二)自定义格式二、时间转字符串(一)使用预定义格式(二)自

Java中Date、LocalDate、LocalDateTime、LocalTime、时间戳之间的相互转换代码

《Java中Date、LocalDate、LocalDateTime、LocalTime、时间戳之间的相互转换代码》:本文主要介绍Java中日期时间转换的多种方法,包括将Date转换为LocalD... 目录一、Date转LocalDateTime二、Date转LocalDate三、LocalDateTim

golang获取当前时间、时间戳和时间字符串及它们之间的相互转换方法

《golang获取当前时间、时间戳和时间字符串及它们之间的相互转换方法》:本文主要介绍golang获取当前时间、时间戳和时间字符串及它们之间的相互转换,本文通过实例代码给大家介绍的非常详细,感兴趣... 目录1、获取当前时间2、获取当前时间戳3、获取当前时间的字符串格式4、它们之间的相互转化上篇文章给大家介

Feign Client超时时间设置不生效的解决方法

《FeignClient超时时间设置不生效的解决方法》这篇文章主要为大家详细介绍了FeignClient超时时间设置不生效的原因与解决方法,具有一定的的参考价值,希望对大家有一定的帮助... 在使用Feign Client时,可以通过两种方式来设置超时时间:1.针对整个Feign Client设置超时时间

springboot+dubbo实现时间轮算法

《springboot+dubbo实现时间轮算法》时间轮是一种高效利用线程资源进行批量化调度的算法,本文主要介绍了springboot+dubbo实现时间轮算法,文中通过示例代码介绍的非常详细,对大家... 目录前言一、参数说明二、具体实现1、HashedwheelTimer2、createWheel3、n

Java实现时间与字符串互相转换详解

《Java实现时间与字符串互相转换详解》这篇文章主要为大家详细介绍了Java中实现时间与字符串互相转换的相关方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、日期格式化为字符串(一)使用预定义格式(二)自定义格式二、字符串解析为日期(一)解析ISO格式字符串(二)解析自定义

Java时间轮调度算法的代码实现

《Java时间轮调度算法的代码实现》时间轮是一种高效的定时调度算法,主要用于管理延时任务或周期性任务,它通过一个环形数组(时间轮)和指针来实现,将大量定时任务分摊到固定的时间槽中,极大地降低了时间复杂... 目录1、简述2、时间轮的原理3. 时间轮的实现步骤3.1 定义时间槽3.2 定义时间轮3.3 使用时