Problem Description
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.
 
Input
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.
 
Output
For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.
 
Sample Input
4 4 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0
 
Sample Output
NO
YES
 #include <stdio.h>
#include <math.h>
char map[][];
int N,M,S_i,S_j,D_i,D_j,Flag;
void found(int,int,int); int main()
{
int i,j,T;
while(scanf("%d%d%d",&N,&M,&T)!=EOF)
{
Flag=;
if(N== && M== && T==) break;
for(i=;i<N;i++)
scanf("%s",map[i]);
for(i=;i<N;i++)
for(j=;j<M;j++)
if(map[i][j]=='S')
{S_i=i;S_j=j;}
else if(map[i][j]=='D')
{D_i=i;D_j=j;}
map[D_i][D_j]='.';
if((int)fabs((double)((S_i+S_j)%-(D_i+D_j)%)) != T%) //奇偶性剪枝
{
puts("NO");
continue;
} //0->1或1->0 必然是奇数步
found(S_i,S_j,T);
printf("%s\n",Flag?"YES":"NO"); //0->0或1->1 必然是偶数步
}
}
void found(int x,int y,int T)
{
map[x][y]='*';
if(T== && x==D_i && y==D_j)
{
Flag=;
/*
printf("\nshow:\n");
for(int i=0;i<N;i++)
printf("%s\n",map[i]);
*/
}
if(T>)
{
if(x+<N&&map[x+][y]=='.') found(x+,y,T-);//这里只能
if(y+<M&&map[x][y+]=='.') found(x,y+,T-);
if(x->=&&map[x-][y]=='.') found(x-,y,T-);
if(y->=&&map[x][y-]=='.') found(x,y-,T-);
}
map[x][y]='.';
}

HDU_1010——小狗走迷宫DFS的更多相关文章

  1. NYOJ306 走迷宫(dfs+二分搜索)

    题目描写叙述 http://acm.nyist.net/JudgeOnline/problem.php?pid=306 Dr.Kong设计的机器人卡多非常爱玩.它经常偷偷跑出实验室,在某个游乐场玩之不 ...

  2. luogu P1238 走迷宫--DFS模板好(水)题

    题目描述 有一个m*n格的迷宫(表示有m行.n列),其中有可走的也有不可走的,如果用1表示可以走,0表示不可以走,文件读入这m*n个数据和起始点.结束点(起始点和结束点都是用两个数据来描述的,分别表示 ...

  3. 我的第一篇博文:C++最初的路-经典的小游戏走迷宫

    写在开始:这个博客建于大二下学期.2年多的学习,从网上借鉴的大牛经验,代码,指导数不胜数,而其中大部分来自别人的博客,于是期待有一天也能把自己在学习过程中的一些经验拿出来与大家分享. 其实我凝望了C+ ...

  4. openjudge走迷宫(DFS)

    题目: 描述 一个迷宫由R行C列格子组成,有的格子里有障碍物,不能走:有的格子是空地,可以走. 给定一个迷宫,求从左上角走到右下角最少需要走多少步(数据保证一定能走到).只能在水平方向或垂直方向走,不 ...

  5. 走迷宫(DFS)

    题目:http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2449&cid=1181 目前dfs 里的递归还是不很懂,AC代码如下: #incl ...

  6. sdut1269 走迷宫(dfs)

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1269 连着做了三个基本的dfs,终于弄懂了搜索 ...

  7. HDU1010 Tempter of the Bone【小狗是否能逃生----DFS奇偶剪枝(t时刻恰好到达)】

    Tempter of the Bone Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  8. sdut 2449走迷宫【最简单的dfs应用】

    走迷宫 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_ 题目描述 一个由n * m 个格子组成的迷宫,起点是(1, 1), 终点是(n, m) ...

  9. HDU 2102 A计划(BFS/DFS走迷宫)

    A计划 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

随机推荐

  1. SAX PULL解析实例

    XML三种解析方式: SAX解析:基于事件驱动,事件机制基于回调函数的,得到节点和节点之间内容时也会回调事件 PULL解析:相同基于事件驱动,仅仅只是回调时是常量 DOM解析:是先把XML文件装入内存 ...

  2. java实现excel的导入导出(poi详解)[转]

    java实现excel的导入导出(poi详解) 博客分类: java技术 excel导出poijava  经过两天的研究,现在对excel导出有点心得了.我们使用的excel导出的jar包是poi这个 ...

  3. linux下用phpize给PHP动态添加扩展(转)

    使用php的常见问题是:编译php时忘记添加某扩展,后来想添加扩展,但是因为安装php后又装了一些东西如PEAR等,不想删除目录重装,别说,php还真有这样的功能. 我没有在手册中看到. 如我想增加b ...

  4. js截取字符串区分汉字字母代码

    js截取字符串并且区分汉字字母,一个汉字辨别为两个字节. function substr(str, len) { if (!str || !len) { return ''; } // 预期计数:中文 ...

  5. Linux shell入门基础(二)

    二.shell对文本的操作 01.查看文本的命令 #cat /etc/passwd(并非对文本文件操作) #tail -5 /etc/passwd(查看末尾5行) #tail -f /var/log/ ...

  6. C#App.config的使用

    为什么使用App.config, 在连接数据的时候将连接字符串写在了类中,如果更换数据库地址,则需要修改这个类,然后重新编译才可以重新连接数据库.在这个时候我们就可以将连接信息放到配置文件App.co ...

  7. spring源码测试

    org.springframework.context.annotation.ComponentScanParserTests.componentScanWithAutowiredQualifier( ...

  8. php function_name($type=0,$order_ids='',$flag=2)

    $order_ids='',表示$order_ids是字符串,不是数组 foreach ($order_ids as $key=>$order_id){            //TODO} 这 ...

  9. gradle命令

    gradle tasks 可以显示gradle可以做的任务. gradle build 就会生成jar包. build和clean install的作用是否一致?

  10. sql Server 常用存储过程的优化

    优化存储过程有很多种方法,下面介绍最常用的7种. 1.使用SET NOCOUNT ON选项 我们使用SELECT语句时,除了返回对应的结果集外,还会返回相应的影响行数.使用SET NOCOUNT ON ...