取自:《王道论坛计算机考研机试指南》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. web服务器的解析漏洞罗列

    前言 服务器相关中间件存在一些解析漏洞,攻击者可通过上传一定格式的文件,被服务器的中间件进行了解析,这样就对系统造成一定危害.常见的服务器解析漏洞涉及的中间件有IIS,apache.nginx等.可利 ...

  2. CF1299D Around the World

    题意 \(n\)阶无向图,\(m\)条带权边,保证\(1\)不会被"超过\(3\)阶的圈"所包含.求删除与\(1\)相邻的边集,使得不存在从\(1\)出发的权值为\(0\)的非平凡 ...

  3. php 字符串常用函数

    数组.字符串和数据库是我们函数里面最.最.最常用的三类函数. 当然PHP的字符串函数也有很多.我们最常使用的两个系列的字符串: 1.单字节字符串处理函数 2.多字节字符串处理函数 3.字符串编码转换函 ...

  4. GYCTF Node game

    考点: NodeJS 代码审计 SSRF 请求夹带 复现: 不太懂js,先留着吧,学懂了再记录

  5. ansible安装-本机测试

    环境:centos7 yum源:网络yum源 安装: 默认yum安装,也可以自己编译安装 yum -y install ansible 本机测试: [root@localhost ~]# ansibl ...

  6. ms17-010 利用msf的exp和一个扫描工具的复现

    0x01简介 永恒之蓝漏洞是方程式组织在其漏洞利用框架中一个针对SMB服务进行攻击的漏洞,该漏洞导致攻击者在目标系统上可以执行任意代码. 攻击对象:win7及win7以下的操作系统且开启了445端口s ...

  7. 欢迎来到L T X的博客 & 博客转型公告

    这里是L T X,一位来自重庆的学生的个人博客. 由于博主以前是OIer,目前博客里主要是OI相关的内容. 但是现在博主已经退役了,因此博客将会转向...嗯...那种...就是那种...比较奇怪的类型 ...

  8. 解决jquery click事件执行两次

    js 解决办法 event.preventDefault() :阻止默认行为,可以用 event.isDefaultPrevented() 来确定preventDefault是否被调用过了 event ...

  9. C++——深拷贝

    要实现深拷贝就需要自己编写拷贝构造函数. 深拷贝 #include<iostream> using namespace std; class Point { public:         ...

  10. static静态不是很静

    在类中定义变量时,不会开辟存储空间,只有类定义一个对象时才会开辟类中成员变量的内存空间,且建立一个对象开辟一次,大小与类中的成员变量及函数有关.而static在静态区开辟内存空间,不占用内存空间. 1 ...