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获取当天的开始和结束时间

《如何利用Java获取当天的开始和结束时间》:本文主要介绍如何使用Java8的LocalDate和LocalDateTime类获取指定日期的开始和结束时间,展示了如何通过这些类进行日期和时间的处... 目录前言1. Java日期时间API概述2. 获取当天的开始和结束时间代码解析运行结果3. 总结前言在J

修改若依框架Token的过期时间问题

《修改若依框架Token的过期时间问题》本文介绍了如何修改若依框架中Token的过期时间,通过修改`application.yml`文件中的配置来实现,默认单位为分钟,希望此经验对大家有所帮助,也欢迎... 目录修改若依框架Token的过期时间修改Token的过期时间关闭Token的过期时js间总结修改若依

Go Mongox轻松实现MongoDB的时间字段自动填充

《GoMongox轻松实现MongoDB的时间字段自动填充》这篇文章主要为大家详细介绍了Go语言如何使用mongox库,在插入和更新数据时自动填充时间字段,从而提升开发效率并减少重复代码,需要的可以... 目录前言时间字段填充规则Mongox 的安装使用 Mongox 进行插入操作使用 Mongox 进行更

对postgresql日期和时间的比较

《对postgresql日期和时间的比较》文章介绍了在数据库中处理日期和时间类型时的一些注意事项,包括如何将字符串转换为日期或时间类型,以及在比较时自动转换的情况,作者建议在使用数据库时,根据具体情况... 目录PostgreSQL日期和时间比较DB里保存到时分秒,需要和年月日比较db里存储date或者ti

windos server2022里的DFS配置的实现

《windosserver2022里的DFS配置的实现》DFS是WindowsServer操作系统提供的一种功能,用于在多台服务器上集中管理共享文件夹和文件的分布式存储解决方案,本文就来介绍一下wi... 目录什么是DFS?优势:应用场景:DFS配置步骤什么是DFS?DFS指的是分布式文件系统(Distr

Python 标准库time时间的访问和转换问题小结

《Python标准库time时间的访问和转换问题小结》time模块为Python提供了处理时间和日期的多种功能,适用于多种与时间相关的场景,包括获取当前时间、格式化时间、暂停程序执行、计算程序运行时... 目录模块介绍使用场景主要类主要函数 - time()- sleep()- localtime()- g

如何用Java结合经纬度位置计算目标点的日出日落时间详解

《如何用Java结合经纬度位置计算目标点的日出日落时间详解》这篇文章主详细讲解了如何基于目标点的经纬度计算日出日落时间,提供了在线API和Java库两种计算方法,并通过实际案例展示了其应用,需要的朋友... 目录前言一、应用示例1、天安门升旗时间2、湖南省日出日落信息二、Java日出日落计算1、在线API2

如何使用 Bash 脚本中的time命令来统计命令执行时间(中英双语)

《如何使用Bash脚本中的time命令来统计命令执行时间(中英双语)》本文介绍了如何在Bash脚本中使用`time`命令来测量命令执行时间,包括`real`、`user`和`sys`三个时间指标,... 使用 Bash 脚本中的 time 命令来统计命令执行时间在日常的开发和运维过程中,性能监控和优化是不

python中的与时间相关的模块应用场景分析

《python中的与时间相关的模块应用场景分析》本文介绍了Python中与时间相关的几个重要模块:`time`、`datetime`、`calendar`、`timeit`、`pytz`和`dateu... 目录1. time 模块2. datetime 模块3. calendar 模块4. timeit

Java将时间戳转换为Date对象的方法小结

《Java将时间戳转换为Date对象的方法小结》在Java编程中,处理日期和时间是一个常见需求,特别是在处理网络通信或者数据库操作时,本文主要为大家整理了Java中将时间戳转换为Date对象的方法... 目录1. 理解时间戳2. Date 类的构造函数3. 转换示例4. 处理可能的异常5. 考虑时区问题6.