取自:《王道论坛计算机考研机试指南》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. 【29】带你了解计算机视觉(Computer vision)

    计算机视觉(Computer vision) 计算机视觉是一个飞速发展的一个领域,这多亏了深度学习. 深度学习与计算机视觉可以帮助汽车,查明周围的行人和汽车,并帮助汽车避开它们. 还使得人脸识别技术变 ...

  2. 1.(group by)如何让group by分组后,每组中的所有数据都显示出来

    问题描述:表如下,如何让这个表按device_id这个字段分组,且组中的每条数据都查寻出来?(假如说这个表名为:devicedata) 错误答案:select * from devicedata GR ...

  3. 专访|高思教育创始人须佶成(上)【UncleW】

    大家好,我是校长运营圈专栏作者UncleW. 2017年9月,高思教育发布董事会公告,宣布完成5.5亿元人民币融资.成立于2009年的高思教育到今天刚刚8岁,员工人数已突破2000人,2017年共有5 ...

  4. java中生成任意之间数的随机数

    public static int cssjs(int a,int b) { Random rand=new Random(); int zhi; zhi=rand.nextInt(b)%(b-a+1 ...

  5. PAT (Basic Level) Practice (中文)1033 旧键盘打字 (20 分)

    旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现.现在给出应该输入的一段文字.以及坏掉的那些键,打出的结果文字会是怎样? 输入格式: 输入在 2 行中分别给出坏掉的那些键.以及应该输入 ...

  6. CSS-定义样式表

    1.HTML标记定义 p{属性:属性值;属性1:属性1} <p>...</p> 注:p可以叫做选择器,定义那个标记中的内容执行其中的样式.一个选择器可以控制若干个样式属性,他们 ...

  7. AcWing 11. 背包问题求方案数

    //g[i,j]表示f[i,j]取最大值的方案数目 //体积最多是j 全部为0,v>=0 //体积恰好为j f[0][0]=0,f[i]=无穷,v>=0 //体积至少是j f[0][0]= ...

  8. 洛谷P1219 八皇后 我。。。。。。

    代码1    (学弟版) #include<bits/stdc++.h>using namespace std;int l[15];bool s[15];                  ...

  9. 跨AppDomain通信

    public class AppDomainTest : MarshalByRefObject { public string TestMethodStr(string srcAppDomain) { ...

  10. SSM项目使用junit单元测试时Mybaties通配符加载Mapper不能正常加载

    个人博客 地址:http://www.wenhaofan.com/article/20181108104133 问题描述 项目使用maven build 以及tomcat run能够正常运行,但是使用 ...