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

相关文章

windos server2022里的DFS配置的实现

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

hdu 2489 (dfs枚举 + prim)

题意: 对于一棵顶点和边都有权值的树,使用下面的等式来计算Ratio 给定一个n 个顶点的完全图及它所有顶点和边的权值,找到一个该图含有m 个顶点的子图,并且让这个子图的Ratio 值在所有m 个顶点的树中最小。 解析: 因为数据量不大,先用dfs枚举搭配出m个子节点,算出点和,然后套个prim算出边和,每次比较大小即可。 dfs没有写好,A的老泪纵横。 错在把index在d

poj 3050 dfs + set的妙用

题意: 给一个5x5的矩阵,求由多少个由连续6个元素组成的不一样的字符的个数。 解析: dfs + set去重搞定。 代码: #include <iostream>#include <cstdio>#include <set>#include <cstdlib>#include <algorithm>#include <cstring>#include <cm

ural 1149. Sinus Dances dfs

1149. Sinus Dances Time limit: 1.0 second Memory limit: 64 MB Let  An = sin(1–sin(2+sin(3–sin(4+…sin( n))…) Let  Sn = (…( A 1+ n) A 2+ n–1) A 3+…+2) An+1 For given  N print  SN Input One

hdu 6198 dfs枚举找规律+矩阵乘法

number number number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description We define a sequence  F : ⋅   F0=0,F1=1 ; ⋅   Fn=Fn

深度优先(DFS)和广度优先(BFS)——算法

深度优先 深度优先搜索算法(英语:Depth-First-Search,DFS)是一种用于遍历或搜索树或图的算法。 沿着树的深度遍历树的节点,尽可能深的搜索树的分支,当节点v的所在边都己被探寻过,搜索将回溯到发现节点v的那条边的起始节点。这一过程一直进行到已发现从源节点可达的所有节点为止。如果还存在未被发现的节点,则选择其中一个作为源节点并重复以上过程,整个进程反复进行直到所有节点都被访

nyoj99(并查集+欧拉路+dfs)

单词拼接 时间限制: 3000 ms  |  内存限制: 65535 KB 难度: 5 描述 给你一些单词,请你判断能否把它们首尾串起来串成一串。 前一个单词的结尾应该与下一个单词的道字母相同。 如 aloha dog arachnid gopher tiger rat   可以拼接成:aloha.arachnid.dog.gopher.rat.tiger 输入 第一行是一个整

【CF】E. Anya and Cubes(双向DFS)

根据题意的话每次递归分3种情况 一共最多25个数,时间复杂度为3^25,太大了 我们可以分2次求解第一次求一半的结果,也就是25/2 = 12,记录结果 之后利用剩余的一半求结果 s-结果 = 之前记录过的结果 就可以 时间复杂度降低为 3 ^ (n/2+1) 题目链接:http://codeforces.com/contest/525/problem/E #include<set

力扣 797. 所有可能路径【DFS】

1. 题目 2. 代码 DFS , 直接见代码 class Solution {public:vector<int> path;vector<vector<int>> res; // 结果集void dfs(vector<vector<int>>& graph, int cur, int n){// 找出所有从节点 0 到节点 n-1 的路径// 下标从 0 开始的if (

CF Bayan 2015 Contest Warm Up B.(dfs+暴力)

B. Strongly Connected City time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output 题目链接: http://codeforces.com/contest/475/probl