题目:

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
 
思想:
奇偶剪枝先,然后dfs,详见代码注释。
 
代码:
 #include<stdio.h>
#include<string.h> int t, flag, startx, starty, endx, endy, cout, visit[][], xx[] = {, -, , }, yy[] = {-, , , };
char a[][]; int dfs(){
int i;
visit[startx][starty] = ;
if(cout == t){
if(startx == endx && starty == endy){
printf("YES\n");
flag = ;
}
return ;
}
for(i = ; i < && flag; i ++){
startx += xx[i];
starty += yy[i];
if(visit[startx][starty] == ){
if(a[startx][starty] != 'X'){
//visit[startx][starty] = 0;
cout ++;
dfs();
visit[startx][starty] = ;//不行后,都要标志成未经过,因为下一个遍历可能遍历到;
cout --; }
startx -= xx[i];
starty -= yy[i];//这两句是当dfs返回后,将x,y变成上一个的值;
}
else{
startx -= xx[i];
starty -= yy[i];
} }
return ;
} int main(){
int n, m, i, j;
while(scanf("%d %d %d", &n, &m, &t) && (n || m || t)){
getchar();
cout = ;
flag = ;
memset(visit, , sizeof(visit));
for(i = ; i <= n; i ++){
for(j = ; j <= m; j ++){
scanf("%c", &a[i][j]);
visit[i][j] = ;
if(a[i][j] == 'S'){
startx = i;
starty = j;
}
else if(a[i][j] == 'D'){
endx = i;
endy = j;
}
}
getchar();
}
//getchar();
if((startx + starty + endx + endy) % ==){
if(t % != ){
printf("NO\n");
continue;
}
}
else{
if(t % == ){
printf("NO\n");
continue;
}
}
dfs();
if(flag == ){
printf("NO\n");
}
}
return ;
}

奋进

杭电1010(dfs + 奇偶剪枝)的更多相关文章

  1. hdoj--1010<dfs+奇偶剪枝>

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目描述:在n*m的矩阵中,有一起点和终点,中间有墙,给出起点终点和墙,并给出步数,在该步数情况 ...

  2. HDU 1010 Tempter of the Bone(DFS+奇偶剪枝)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意: 输入 n m t,生成 n*m 矩阵,矩阵元素由 ‘.’ 'S' 'D' 'X' 四 ...

  3. hdu.1010.Tempter of the Bone(dfs+奇偶剪枝)

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  4. hdu 1010:Tempter of the Bone(DFS + 奇偶剪枝)

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  5. HDU 1010:Tempter of the Bone(DFS+奇偶剪枝+回溯)

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  6. hdu1010Tempter of the Bone(dfs+奇偶剪枝)

    题目链接:pid=1010">点击打开链接 题目描写叙述:给定一个迷宫,给一个起点和一个终点.问是否能恰好经过T步到达终点?每一个格子不能反复走 解题思路:dfs+剪枝 剪枝1:奇偶剪 ...

  7. Tempter of the Bone(dfs奇偶剪枝)

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  8. Tempter of the Bone(dfs+奇偶剪枝)题解

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  9. M - Tempter of the Bone(DFS,奇偶剪枝)

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

随机推荐

  1. PERL 实现微信登录

    get 请求: https://login.weixin.qq.com/jslogin? appid=wx782c26e4c19acffb &redirect_uri=https%3A%2F% ...

  2. 工具:七牛云备份VPS服务器文件

    每一天,服务器上都会产生一系列的数据文件,有些文件不免具有重要性,但是如果我们只是简单的移动文件,那将不会具有很好的备份性,这里,我们借助Python实现对于文件的云端备份.      这个程序是我利 ...

  3. grep命令实例

    grep一般用于查找文件中含有某些字符串的行,其命名格式如下 grep [OPTIONS] PATTERN [FILE...] 下面例举grep在linux使用过程中其常用使用实例: 1.grep递归 ...

  4. c++ 友元类

    一.友元类相关概念 要将私有成员数据或函数暴露给另一个类,必须将后者声明为友元类. 注意三点: (1)友元关系不能传递 (2)友元关系不能继承 (3)友元关系不能互通

  5. IO之内核buffer----"buffer cache"

    举例 一般情况下,Read,write系统调用并不直接访问磁盘.这两个系统调用仅仅是在用户空间和内核空间的buffer之间传递目标数据. 举个例子,下面的write系统调用仅仅是把3个字节从用户空间拷 ...

  6. 【转】Alsa音频编程【精华】

    一.前序 这里了解一下各个参数的含义以及一些基本概念. 声音是连续模拟量,计算机将它离散化之后用数字表示,就有了以下几个名词术语. 样本长度(sample):样本是记录音频数据最基本的单位,计算机对每 ...

  7. HTTP协议4之缓存--转

    HTTP协议提供了非常强大的缓存机制, 了解这些缓存机制,对提高网站的性能非常有帮助. 缓存的概念 缓存这个东西真的是无处不在, 有浏览器端的缓存, 有服务器端的缓存,有代理服务器的缓存, 有ASP. ...

  8. EffectiveC#18--用IComparable和IComparer实现对象的顺序关系

    1..Net框架提供了接口来描述对象的顺序关系:IComparable 和IComparer. 2.IComparable 为类定义了自然顺序,实现IComparer接口的类可以描述其它可选的顺序 3 ...

  9. jquery 图片比例不变,全屏居中

    <!DOCTYPE html> <html> <head> <meta charset="gb2312"> <title> ...

  10. SqlServer存储过程传入Table参数

    今天是周日,刚好有空闲时间整理一下这些天工作业务中遇到的问题. 有时候我们有这样一个需求,就是在后台中传过来一个IList<类>的泛型集合数据,该集合是某个类的实例集合体,然后将该集合中的 ...