取自:《王道论坛计算机考研机试指南》6.5节

例 6.7 Temple of the bone(九度 OJ 1461)
时间限制:1 秒 内存限制:32 兆 特殊判题:否
题目描述:
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.
输入:
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

代码:

#include <stdio.h>
#include <cstring>
const int maxn = ;
char maze[maxn][maxn]; //迷宫数组
int status[maxn][maxn] = { };
int n, m, t;
bool flag = false; //是否有解的标记 //四个方向
int dir[][] = { { -, }, { , }, { , - }, { , } }; void dfs(int row, int col, int nowK){
//边界条件
if (maze[row][col] == 'D' && nowK == t){
flag = true;
return;
} if (nowK > t) return; //把这个位置标记为已访问
status[row][col] = ; int newRow, newCol;
for (int i = ; i < ; i++){
newRow = row + dir[i][];
newCol = col + dir[i][];
if (newRow < n && newRow >= && newCol < m && newCol >= && maze[newRow][newCol] != 'X' && status[newRow][newCol] == )
dfs(newRow, newCol, nowK + );
} } int main()
{
//freopen("in.txt", "r", stdin);
//读取maze数组,读取的过程中获取S的位置
while (true){
scanf("%d %d %d", &n, &m, &t);
if ( == n && == m && == t){
break;
} int row, col; //记录doggie的初始位置 //重新初始化status数组
memset(status, , sizeof(status));
flag = false;
getchar(); //读取回车 for (int i = ; i < n; i++){
for (int j = ; j < m; j++){
scanf("%c", &maze[i][j]);
if ('S' == maze[i][j]){
row = i;
col = j;
} }
getchar(); //读取回车
} //dfs
dfs(row, col, ); //输出
if (flag){
printf("YES\n");
}
else{
printf("NO\n");
}
} //fclose(stdin);
return ;
}

dfs题型二(迷宫问题)的更多相关文章

  1. (DFS)P1605 迷宫 洛谷

    题目背景 迷宫 [问题描述] 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和 终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案.在迷宫 中移动有上下 ...

  2. 万能的搜索--之DFS(二)

    (一)深度优先搜索(DFS) 我们先给出深度优先的解决办法,所谓深度优先搜索,在迷宫问题里就是不撞南墙不回头,能走得深一点就尽量深一点.如果碰到了墙壁就返回前一个位置尝试其他的方向.在<啊哈!算 ...

  3. 【DFS】NYOJ-82 迷宫寻宝(一)-条件迷宫问题

    [题目链接:NYOJ-82] #include<iostream> #include<cstring> using namespace std; struct node{ in ...

  4. 深度优先搜索DFS(二)

    总结下图里面的常用模板: DFS(u){ vis[u]=true; for(从u出发能到达的所有顶点v){ if(vis[v]==false){ DFS(v); } } } DFSTrave(G){ ...

  5. DFS(二):骑士游历问题

    在国际象棋的棋盘(8行×8列)上放置一个马,按照“马走日字”的规则,马要遍历棋盘,即到达棋盘上的每一格,并且每格只到达一次.例如,下图给出了骑士从坐标(1,5)出发,游历棋盘的一种可能情况. [例1] ...

  6. CF982C Cut 'em all! DFS 树 * 二十一

     Cut 'em all! time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  7. dfs题型一

    代码: #include <iostream> #include <algorithm> #include <vector> using namespace std ...

  8. 自动生成数学题型二(框架struts2)题型如((a+b)*c=d)

    1. 生成题目 1.1 生成单个题目 public static String[] twoOperatorAndOperator(int num1, int num2) { double first ...

  9. Javascript小白经典题型(二)

    51. 输出的是什么? function getInfo(member, year) { member.name = "Lydia"; year = "1998" ...

随机推荐

  1. super().__init__()方法

    class first(object): def __init__(self,age,name): self.age = age self.name = name class second(first ...

  2. c语言中 char* 和 unsigned char* 的区别浅析(转)

    原文:https://blog.csdn.net/guotianqing/article/details/77341657 背景最近在项目中遇到了一个编译警告,是因为定义的变量为char[],而在使用 ...

  3. WebMethods开发入门

    webMethods  Integration Platform 由用于设计.执行和管理集成解决方案的 3 类组件构成. 设计时组件:这些组件提供了开发和测试集成解决方案的工具. 1.webMetho ...

  4. 题解 P5718 【【深基4.例2】找最小值】

    题目传送门 思路 介绍一种新方法--sort排序,它的格式是这样的sort(a+1,a+n+1,cmp);,我们只需要把a数组排好序,然后输出第\(1\)个元素即可. 定义a数组与变量\(n\)并输入 ...

  5. 初入python

    初入python 一定要学好python 求1-100的和: i=1 s=0 while i<101: s=s+i i=i+1 print(s)

  6. LeetCode Two Sum&Two Sum II - Input array is sorted&3Sum&4Sum 一锅煮题解

    文章目录 Two Sum Two Sum II 3Sum 4Sum Two Sum 题意 给定一个数组,和指定一个目标和.从数组中选择两个数满足和为目标和.保证有且只有一个解.每个元素只可以用一次. ...

  7. 如何处理pom文件中没有找到HUB检查到高危漏洞的依赖包

    最近使用HUB工具检查到maven工程中存在高危险漏洞,虽然定位到具体的引用包了,但是在pom文件中却没有发现该依赖包.此时,我们就需要用到这条命令mvn dependency:tree,该命令会将m ...

  8. JS编解码与Java编解码的对应关系

    最近前段在导出数据时会遇到“illegal character”的异常错误,结果发现是在请求地址中请求参数包含了空白字符(其编码为%C2%A0)或者是空格字符(其编码为%20),之前对空格字符情况是做 ...

  9. Help Jimmy POJ - 1661 dp

    #include<iostream> #include<stdio.h> #include<algorithm> #include<cstring> u ...

  10. 【剑指Offer】44、翻转单词顺序列

    题目描述 牛客最近来了一个新员工Fish,每天早晨总是会拿着一本英文杂志,写些句子在本子上.同事Cat对Fish写的内容颇感兴趣,有一天他向Fish借来翻看,但却读不懂它的意思.例如,“student ...