取自:《王道论坛计算机考研机试指南》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 ;
}

最新文章

  1. C和指针 第十六章 标准函数库
  2. Source Insight 3.X utf8支持插件更新
  3. 【算法与数据结构】冒泡、插入、归并、堆排序、快速排序的Java实现代码
  4. eclipse 设置书签标记(标签-Bookmark
  5. position置顶或某固定位置 兼容ie6ie7
  6. Linux 配置主机名
  7. PM2源码浅析
  8. 10.Flask上下文
  9. PyCharm中 Django1.11配置Mysql数据库
  10. 六、uboot 代码流程分析---start.S
  11. python sqlite3 数据库操作
  12. MySQL学习(四)
  13. 外购半成品回写PR时将同一供应商同一编码的PR合并数量回写
  14. HDFS概要
  15. httpd: Could not reliably determine the server&#39;s fully qualified domain name(转)
  16. linux 查看系统编码和修改系统 编码方法
  17. 76. Minimum Window Substring (String, Map)
  18. Java设计模式—原型模式
  19. Name与x:Name的关系
  20. 图像处理之Canny边缘检測

热门文章

  1. JavaDay9(下)
  2. 实现Windows数据更新
  3. 二叉堆(1)BinaryHeap
  4. C 库函数 - modf()
  5. itchat 爬了爬自己的微信通讯录
  6. new SparkContext()发生错误java.lang.NoSuchMethodError: scala.Predef
  7. Xamarin.Android 开发,生成时提示“Resource.Drawable”未包含“BG”的定义
  8. linux中Jenkins启动/重启/停止命令
  9. 第一篇 网站基础知识 第7章 Tomcat分析
  10. 解决lucene更新删除无效的问题