Temple of the bone(dfs)

2023-11-20 20:10
文章标签 dfs bone temple

本文主要是介绍Temple of the bone(dfs),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目描述:

The doggie found a bone in an ancient maze, which fascinated him a lot.

doggi发现一个古代迷宫里面的骨头,这很吸引他。

However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking.

然而,当他拿起,迷宫开始摇晃,dog可能会在下沉的地面跌落

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.

迷宫是个N*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).

一开始,门是关着的。  他会在T-th秒的时刻 打开,持续少于1s的很短的时间

Therefore the doggie had to arrive at the door on exactly the T-th second.

他需要精确到达,在T-th秒这个时刻

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.

一旦他进入一个block,他这个砖块的地面会下沉 消失,在下一秒。

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.

一个砖块,最多带一秒钟。不能到访问过的砖块。

输入:

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. 

输出:

For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.

样例输入:

4 4 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0


样例输出:

NO
YES

 

 

题目大意:有一个 N*M 的迷宫,包括起点 S,终点 D,墙 X,和地面.

0秒时主人公从 S 出发,每秒能走到四个与其相邻的位置中的一个,且每个位置被行走之后都不能再次走入,问是否存在这样一条路径使主人公在 T 秒时刚好走到 D。

 

他这个dfs找到出口还不行,还要到出口时,必须是t时刻。

 

 

#include <iostream>
using namespace std;int n,m;
char maze[11][11];
bool mark[11][11];
int time;
int startY,startX;
int endY,endX;bool isOk = false;bool isInMap(int y,int x) {if ( y >= 1 && y <= n && x >= 1 && x <= m) {return true;}return false;
}void dfs(int y, int x,int t) {
//	cout << "目前来到了位置y and x:" << y << "   " << x << "  ,time : " << t << endl;int ny,nx;// 下if (!isOk) {ny = y + 1;nx = x;
//		cout  << y << " " << x <<"的下:" << ny << " " << nx << ", 情况: " << isInMap(ny,nx) << maze[ny][nx]<< mark[ny][nx] << endl;if (isInMap(ny,nx) && maze[ny][nx] == '.' && mark[ny][nx] == 0) {mark[ny][nx] = 1;dfs(ny,nx,t+1);mark[ny][nx] = 0;}if (isInMap(ny,nx) && maze[ny][nx] == 'D') {if (t + 1 == time) {
//				cout  << "ok!" << endl;isOk = true;return;} else {
//				cout << "time 不对!" << endl;}}}//上if (!isOk) {ny = y - 1;nx = x;
//		cout  << y << " " << x <<"的上:" << ny << " " << nx << ", 情况: " << isInMap(ny,nx) << maze[ny][nx]<< mark[ny][nx] << endl;if (isInMap(ny,nx) && maze[ny][nx] == '.' && mark[ny][nx] == 0) {mark[ny][nx] = 1;dfs(ny,nx,t+1);mark[ny][nx] = 0;}if (isInMap(ny,nx) && maze[ny][nx] == 'D') {if (t + 1 == time) {
//				cout  << "ok!" << endl;isOk = true;return;} else {
//				cout << "time 不对!" << endl;}}}//左if (!isOk) {ny = y;nx = x - 1;
//		cout  << y << " " << x <<"的左:" << ny << " " << nx << ", 情况: " << isInMap(ny,nx) << maze[ny][nx]<< mark[ny][nx] << endl;if (isInMap(ny,nx) && maze[ny][nx] == '.' && mark[ny][nx] == 0) {mark[ny][nx] = 1;dfs(ny,nx,t+1);mark[ny][nx] = 0;}if (isInMap(ny,nx) && maze[ny][nx] == 'D') {if (t + 1 == time) {
//				cout  << "ok!" << endl;isOk = true;return;} else {
//				cout << "time 不对!" << endl;}}}//右if (!isOk) {ny = y;nx = x + 1;
//		cout  << y << " " << x <<"的右:" << ny << " " << nx << ", 情况: " << isInMap(ny,nx) << maze[ny][nx]<< mark[ny][nx] << endl;if (isInMap(ny,nx) && maze[ny][nx] == '.' && mark[ny][nx] == 0) {mark[ny][nx] = 1;dfs(ny,nx,t+1);mark[ny][nx] = 0;}if (isInMap(ny,nx) && maze[ny][nx] == 'D') {if (t + 1 == time) {
//				cout  << "ok!" << endl;isOk = true;return;} else {
//				cout << "time 不对!" << endl;}}}}int main() {while(cin >> n >> m >> time && n != 0 && m != 0 && time != 0) {// cout << "n m t : " << n << m << t << endl;isOk = false;for (int i = 1; i <= n; i++) {for (int j = 1; j <= m; j++) {cin >> maze[i][j];mark[i][j] = 0;if(maze[i][j] == 'S') {startY = i;startX = j;}if(maze[i][j] == 'D') {endY = i;endX = j;}}}// cout << "***" << startY << "   " << startX << endl;// cout << "***" << endY << "   " << endX << endl;dfs(startY, startX,0);if (isOk) {cout << "YES" << endl;} else {cout << "NO" << endl;}}
}

 

#include <iostream>
using namespace std;char maze[101][101];
bool mark[101][101];
int yMax,xMax;
int time;bool isFound;int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}
};bool isInMap(int y,int x) {if (y <= yMax && y >= 1 && x >= 1 && x <= xMax) {return 1;}return 0;
}void dfs(int y,int x,int t) {
//	cout << "current:" << y << " "<< x << " "<< t << " " << maze[y][x] << endl;if (t > time) {return ;}if (maze[y][x] == 'D' && t == time) {
//		cout << "ok" << endl;isFound = true;return ;}if (isFound == true) {return ;} else {int ny ,nx;for (int i = 0; i <= 3; i++) {ny = y + dir[i][0];nx = x + dir[i][1];if (isInMap(ny,nx) == 1 && (maze[ny][nx] == 'D'  || maze[ny][nx] == '.') && mark[ny][nx] == 0) {mark[ny][nx] = 1;dfs(ny,nx,t+1);mark[ny][nx] = 0;}}}
}int main() {while (cin >> yMax >> xMax >> time) {if (yMax == 0 && xMax == 0 && time == 0){return 0;}int startY;int startX;isFound = false;for (int i = 1; i <= yMax; i++) {for (int j = 1; j <= xMax; j++) {cin >> maze[i][j];mark[i][j] = 0;if (maze[i][j] == 'S') {startY = i;startX = j;}}}dfs(startY,startX,0);if (isFound == true) {cout << "YES" << endl;} else {cout << "NO" << endl;}}}

 

这篇关于Temple of the bone(dfs)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

poj 1564 Sum It Up -- DFS 递归

题意:给一个数 t ,以及 n 个数,求 n 个数中的几个数加起来的和为 t 的情况有多少种。 注意:题目要求相同的组合方式不能出现2次,即 “3 4 1 1 1 1 ” 的结果为:“1+1+1”。 思路:一个  for  循环遍历一遍,每个 i 表示以当前数为起点开始一次DFS递归,当所遍历的和为 t 时,输出该组合方式,如果大于 t 则返回,小于则往下递归。以二维数组保存已经输出过的数据,

洛谷 P1141 01迷宫 (dfs解决)

题目描述 有一个仅由数字 0 与 1 组成的 n×n 格迷宫。若你位于一格 0 上,那么你可以移动到相邻 4 格中的某一格 1 上,同样若你位于一格 1 上,那么你可以移动到相邻 4 格中的某一格 0 上。 你的任务是:对于给定的迷宫,询问从某一格开始能移动到多少个格子(包含自身)。 输入格式 第一行为两个正整数 𝑛,𝑚。 下面 𝑛 行,每行 𝑛 个字符,字符只可能是 0 或者

[HDU 4324] Triangle LOVE (拓扑排序,DFS)

HDU - 4324 题意是,一张有 N个点的图,保证每两个点之间有且只有一条有向边连接 求是否存在三元环 用拓扑排序判环,如果存在环,则一定存在三元环 证明如下: 不存在二元环 设存在 n(n>=3)元环 p1->p2->p3->…->pn->p1 1) 若存在边 p3->p1,则存在三元环 (p1->p2->p3->p1) 2) 若不存在 p3->p1,则必然存在 p1->p3

[CSU - 1811 (湖南省赛16)] Tree Intersection (dfs序维护子树+离线询问+树状数组)

CSU - 1811 (湖南省赛16) 给定一棵树,每个节点都有一个颜色 问割掉任意一条边,生成的两个子树中颜色集合的交集大小 这个是 dfs序处理子树 + 离线询问 + bit维护 的解法 首先问题转化为求解一个子树中有多少种颜色以及独有颜色的数量 用总的颜色数量减去独有颜色数量即为这棵子树的答案 先做一遍 dfs,再按 dfs序重新组建颜色序列 这样对子树的询问,就变成

DFS走迷宫(懒猫老师C++完整版)

DFS走迷宫的C++完整版 知识储备一:普通动态二维数组的构造二:栈的构造三:栈的逆序遍历 Main文件代码 该版本代码是配合 懒猫老师用DFS走迷宫视频 基于C++基础所写的完整版(实现了栈逆序遍历),适合小白系统性的学习和复习知识点,只要将下文.h和.cpp和main文件所展示的代码结合起来就是个完整的代码。 知识储备 一:普通动态二维数组的构造 二维数组中不同行的内

fast DFS 单机使用实例

fast DFS 单机使用实例 我在一台服务器上简单测试了fastdfs。client, tracker, storage server都是同一个物理服务器。   1. 编译fastdfs:   sles207:/opt/mars/FastDFS # ./make.sh   storage_service.o: In function `storage_service_init':/opt/ma

[深度优先搜索DFS]迷宫问题

描述 定义一个二维数组: int maze[5][5] = {0, 1, 0, 0, 0,0, 1, 0, 1, 0,0, 0, 0, 0, 0,0, 1, 1, 1, 0,0, 0, 0, 1, 0,}; 它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。 输入 一个5 × 5的二维数组,表示一个迷宫。数

【LeetCode】 Surrounded Regions (BFS DFS)

题目:Surrounded Regions 广搜和深搜都能解决,但是LeetCode上使用深搜时会栈溢出 DFS: <span style="font-size:18px;">/*LeetCode Surrounded Regions* 题目:给定一个字符数组,由'X'和'O'组成,找到所有被x包围的o并将其替换为x* 思路:只要替换被包围的o就行,如果有一个o是边界或者上下左右中有一个

HDU 1044 BFS+DFS

先BFS出每个点之间的最小距离 然后DFS最优值 #include "queue"#include "iostream"#include "algorithm"using namespace std;int dir[4][2]={1,0,-1,0,0,1,0,-1};struct node{int x,y,step;};struct comp{int x,y;} mar

DFS(一)

问题一(指数级选择) 从1~n这n个整数中任意选取多个,输出所有可能的选择方案。 首先想一下,在现实世界中,我们要如何解决这个问题。 应该是一个一个枚举,即每个数都可以有两个选择(选/不选)。共有种结果。 想一下,如何在“纸上”解决问题呢?不妨假设有3各数(分别为1、2、3)。 一共有3个位置,从第一个位置开始枚举选还是不选,这里只列出了一半。那么在编程语言上如何实现呢? 仔细想一