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. CloudFoundry Service 使用

    Mysql服务在V2版本号中github上有独立的releaseproject(cf-mysql-release),该release提供了一个Mysql-broker和一个Mysql-server和( ...

  2. hdu 4499 Cannon(暴力)

    题目链接:hdu 4499 Cannon 题目大意:给出一个n*m的棋盘,上面已经存在了k个棋子,给出棋子的位置,然后求能够在这种棋盘上放多少个炮,要求后放置上去的炮相互之间不能攻击. 解题思路:枚举 ...

  3. 深入理解ob_flush和flush的区别(转)

    ob_flush/flush在手册中的描述, 都是刷新输出缓冲区, 并且还需要配套使用, 所以会导致很多人迷惑… 其实, 他们俩的操作对象不同, 有些情况下, flush根本不做什么事情.. ob_* ...

  4. redis 本机链接服务端命令

    在windows 本机链接服务端redis,需要下载windows 端的redis: 1,运行redis-server.exe程序:2,打开cmd 控制台3,执行命令 D:\redis64\redis ...

  5. Java多线程——ThreadLocal类

    一.概述   ThreadLocal是什么呢?其实ThreadLocal并非是一个线程的本地实现版本,它并不是一个Thread,而是threadlocalvariable(线程局部变量).也许把它命名 ...

  6. SpringMVC03controller中定义多个方法

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...

  7. C#中的序列化与反序列化

    眼看XX鸟的课程关于C#的知识点就要学完了,翻看网络中流传的教程还是发现了一个课程中没有讲到的知识点:序列化与反序列化 无奈还是了解一下并操作一番,以备后用吧 介绍:就是将对象信息转化为二进制信息以便 ...

  8. POJ1502

    #include <iostream> #include <cstdio> #include <algorithm> #include <climits> ...

  9. ubuntu 安装 maven3

    sudo add-apt-repository ppa:natecarlson/maven3 sudo apt-get update && sudo apt-get install m ...

  10. C++:MEMSET的大坑三两事

    之前写了一题费用流,竟然硬是在写SPFA时为DIS数组赋初始值用了MEMSET数组QAQ 调试了很久也没有弄明白自己是卡在那里了,,,感觉被自己蠢哭了QWQ 错误的姿势!! #include < ...