dfs题型二(迷宫问题)
取自:《王道论坛计算机考研机试指南》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题型二(迷宫问题)的更多相关文章
- (DFS)P1605 迷宫 洛谷
题目背景 迷宫 [问题描述] 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和 终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案.在迷宫 中移动有上下 ...
- 万能的搜索--之DFS(二)
(一)深度优先搜索(DFS) 我们先给出深度优先的解决办法,所谓深度优先搜索,在迷宫问题里就是不撞南墙不回头,能走得深一点就尽量深一点.如果碰到了墙壁就返回前一个位置尝试其他的方向.在<啊哈!算 ...
- 【DFS】NYOJ-82 迷宫寻宝(一)-条件迷宫问题
[题目链接:NYOJ-82] #include<iostream> #include<cstring> using namespace std; struct node{ in ...
- 深度优先搜索DFS(二)
总结下图里面的常用模板: DFS(u){ vis[u]=true; for(从u出发能到达的所有顶点v){ if(vis[v]==false){ DFS(v); } } } DFSTrave(G){ ...
- DFS(二):骑士游历问题
在国际象棋的棋盘(8行×8列)上放置一个马,按照“马走日字”的规则,马要遍历棋盘,即到达棋盘上的每一格,并且每格只到达一次.例如,下图给出了骑士从坐标(1,5)出发,游历棋盘的一种可能情况. [例1] ...
- CF982C Cut 'em all! DFS 树 * 二十一
Cut 'em all! time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- dfs题型一
代码: #include <iostream> #include <algorithm> #include <vector> using namespace std ...
- 自动生成数学题型二(框架struts2)题型如((a+b)*c=d)
1. 生成题目 1.1 生成单个题目 public static String[] twoOperatorAndOperator(int num1, int num2) { double first ...
- Javascript小白经典题型(二)
51. 输出的是什么? function getInfo(member, year) { member.name = "Lydia"; year = "1998" ...
随机推荐
- Selenium实战(三)——滑动解锁+窗口截图
一.简单滑块的实例 当单机滑块时,改变的只是CSS样式,HTML代码如下: <div class="slide-to-unlock-progress" style=" ...
- 树莓派3b 换国内源 更新源
在国内要更新源的时候,因为是国外的源,总会出现网速太慢的问题, 以下是对于安装了,2017-11-29-raspbian-stretch 系统源 更换最好用root登陆操作 sudo passwd r ...
- Qt Gui 第五章绘图类
双缓冲 void Plotter::refreshPixmap() { pixmap = QPixmap(size()); pixmap.fill(, ); QPainter painter(& ...
- 0级搭建类001-RedHat Enterprise Linux 8 安装(RHEL 8) 公开
项目文档引子系列是根据项目原型,制作的测试实验文档,目的是为了提升项目过程中的实际动手能力,打造精品文档AskScuti. 项目文档引子系列目前不对外发布,仅作为博客记录.如学员在实际工作过程中需提前 ...
- 第三章:使用ListView展示数据
一.ImageList:存储图像集合 Images 存储的所有图像 ImageSize 图像的大小 ColorDepth 颜色数 TransparentColor 被视为透明的颜色 先设置ColorD ...
- DFS-B - Dr. Evil Underscores
B - Dr. Evil Underscores Today, as a friendship gift, Bakry gave Badawy nn integers a1,a2,…,ana1,a2, ...
- 微信小程序 --- 日历效果
wxml部分: <view class='box1' style='width: {{ sysW * 7 }}px'> <view class='dateBox'>{{ yea ...
- 一直报找不到function,然后又不为null。最后发现是个数组。哭死。
今天写代码,报找不到function.然后又不为空.最后发现是类型不对.因为是数组,所以不能用node.function(),而应该用nodeArr[0].function.错用前者会找不到funct ...
- bfs(队列模板)
[题目描述] 当你站在一个迷宫里的时候,往往会被错综复杂的道路弄得失去方向感,如果你能得到迷宫地图,事情就会变得非常简单. 假设你已经得到了一个n*m的迷宫的图纸,请你找出从起点到出口的最短路. [输 ...
- Windows7下Docker的安装
转自 https://blog.csdn.net/xiangxiezhuren/article/details/79698913 无法打开图3,打开属性.给其添加git路径 无法使用图2下载 h ...