Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 55541    Accepted Submission(s): 14983

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
 
Author
ZHANG, Zheng
 
Source
 


      
题目大意:
要求每个点只能走一次,从起点走到终点刚好sum步,点表示可以走得路经。S,D分别代表起点终点。

解题思路:每个点只能走一次,所以必须回溯。开始没想到剪枝,果断TLE了,而且因为yes,no的大小写还WA了一发脑残了。

        题目地址:Tempter of the Bone

AC代码:
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdio>
using namespace std;
char a[10][10];
int visi[10][10];
int n,m,sum,flag;
struct node
{
int x;
int y;
};
node sta,en; //起点与终点
int dir[4][2]= //四个方向
{
{-1,0},{1,0},{0,-1},{0,1}
}; void dfs(int cx,int cy,int step)
{
int i;
if(flag==1) return; //找到解
if(step==sum)
{
if(cx==en.x&&cy==en.y)
flag=1;
return;
}
int need=abs(cx-en.x)+abs(cy-en.y);
int tmp=sum-step;
if((tmp%2==0&&need%2==1)||(need%2==0&&tmp%2==1)||tmp<need) //奇偶剪枝
return;
for(i=0;i<4;i++)
{
int px,py;
px=cx+dir[i][0],py=cy+dir[i][1];
if(px>=0&&px<n&&py>=0&&py<m&&(a[px][py]=='.'||a[px][py]=='D')&&!visi[px][py])
{
visi[px][py]=1;
dfs(px,py,step+1);
visi[px][py]=0; //回溯
}
}
//return;
} int main()
{
int i,j;
while(scanf("%d%d%d",&n,&m,&sum))
{
if(n==0&&m==0&&sum==0)
break;
for(i=0;i<n;i++)
scanf("%s",a[i]);
flag=0;
for(i=0;i<n;i++) //找起点
{
for(j=0;j<m;j++)
{
if(a[i][j]=='S')
{
sta.x=i; sta.y=j;
flag=1; break;
}
}
if(flag) break;
}
flag=0;
for(i=0;i<n;i++) //找终点
{
for(j=0;j<m;j++)
{
if(a[i][j]=='D')
{
en.x=i; en.y=j;
flag=1; break;
}
}
if(flag) break;
} flag=0;
memset(visi,0,sizeof(visi));
visi[sta.x][sta.y]=1;
dfs(sta.x,sta.y,0);
if(flag) puts("YES");
else puts("NO");
}
return 0;
} //703MS 228K

HDU 1010Tempter of the Bone(奇偶剪枝回溯dfs)的更多相关文章

  1. 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 ...

  2. hdu 1010 Tempter of the Bone 奇偶剪枝

      如果所给的时间(步数) t 小于最短步数path,那么一定走不到. 若满足t>path.但是如果能在恰好 t 步的时候,走到出口处.那么(t-path)必须是二的倍数. 关于第二种方案的解释 ...

  3. zoj 2110 很好的dfs+奇偶剪枝

    //我刚开始竟然用bfs做,不断的wa,bfs是用来求最短路的而这道题是求固定时间的 //剪纸奇偶剪枝加dfs #include<stdio.h> #include<queue> ...

  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. hdu 1010 Tempter of the Bone(深搜+奇偶剪枝)

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

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

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

  8. 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 ...

  9. hdu Tempter of the Bone (奇偶剪枝)

    学习链接:http://www.ihypo.net/1554.html https://www.slyar.com/blog/depth-first-search-even-odd-pruning.h ...

随机推荐

  1. 全选demo

    我们处理数据时,最好能够支持全选操作. 选中之后,进行删除,或其他处理. 我自己写了一个demo. 主要功能: 1.点击全部选中 2.点击全部取消 3.然后进行获取选中的id,进行处理 代码如下: & ...

  2. poj 3254 Corn Fields_状态压缩dp

    感谢:http://www.cnblogs.com/ka200812/archive/2011/08/11/2135607.html 让我搞懂了. #include <iostream> ...

  3. mac系统奔溃无法启动时,如何备份重要资料

    虽然说苹果系统以稳定性获得高度好评,但是作为一名程序员,还是要考虑到系统奔溃的情况. 当遇到系统奔溃,无法启动,而我们还没有备份电脑里面的重要资料,这时候不用着急.可以用下面的 方法来拯救你的苹果电脑 ...

  4. Appium for Windows环境搭建

    服务环境: 1.安装Nodejs 下载nodejs安装包(http://nodejs.org/download/)安装 测试安装是否成功:运行cmd,输入node -v 2.安装android的SKD ...

  5. 反对抄袭 正解spring的@Autowired 不要相信网上的错误版本

    首先,最重要的, @Autowired的就是用来来消除 set ,get方法. 有些介绍,如著名的马士兵,说要在set方法上进行注入.我当时就看不明白了,既然只取消了一个GET,这个@Autowire ...

  6. 实现在Android 多点手势识别

    google 提供的API中,有个类,大家都很熟悉,GestureDetector.使用它,我们可以识别用户通常会用的手势.但是,这个类不支持多点触摸(可能 google认为没有人会在几个手指都在屏幕 ...

  7. Bellman 算法

    这道题目事实上就是在求有没有正环.与求负环的差别就是要不断的更新值,可是这个值要变大.而不是变小. Currency Exchange Time Limit: 1000MS   Memory Limi ...

  8. Bostonkey Simple calc

    Simple Calc 明显的memcpy栈溢出,是一个静态链接的程序所以没给libc.发现里面有: 参数a1应该为_libc_stack_end的地址了._stack_prot通过rop修改为0x7 ...

  9. CSS3 布局

    1.1 列布局   CSS3中新出现的多列布局(multi-column)是传统HTML网页中块状布局模式的有力扩充.这种新语法能够让WEB开发人员轻松的让文本呈现多列显示.我们知道,当一行文字太长时 ...

  10. Android学习之菜单

    android中包含多种菜单,本例带来的是选项菜单和上下文菜单. 1.选项菜单 在android中,开发者可以在xml文档中部署所要添加的菜单,在后台调用即可. <menu xmlns:andr ...