Tempter of the Bone

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

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
 
 
这个题主要是用深搜,看所有路径中有没有步数为给出的值的!
但是注意只用搜索一定会超时!!!
还要用 奇偶剪枝!
 
 
下面我讲一下奇偶剪枝
    
一起看这个图,要从S到E,如果没有障碍物#,最短的路径是6步!
但是有障碍物之后,我们就要绕路走,这时候的步数可以分为两部分  1.最短路径部分
                               2.走出最短路径的步数加上走回最短路径的步数(注意,走出去和走回的步数是相等的)
 
 
解释一下为什么会相等
 
 
看这个图,最短路径是黑色部分,现在要走红色部分,那么走出和走回最短路径的部分就是红色部分,为什么不算上蓝色的呢,因为如果把红色去掉,蓝色部分平移后就是最短路径!!
所以说,要想t步走到终点,多走的部分一定是偶数     即是(t-最短步数)一定为偶数,所以,我们就能剪枝了!
 
 
 
 #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char map[][];
int m,n,bx,by,ex,ey,step,t,flag;
int mov[][]={,,,-,,,-,}; bool can(int x,int y)
{
if(x<||x>m-||y<||y>n-||map[x][y]=='X')
return false;
return true;
}
void DFS(int x,int y)
{ int xx,yy,i;
if(x==ex&&y==ey)//判断是否找到终点
{
if(step==t)
flag=;
return ;
}
if(step>t)
return ;
int dis=t-abs(ex-x)-abs(ey-y)-step;
if(dis<||dis&)//奇偶剪枝
return ;
if(flag==)
return ;//当初我就是没加这一句,在hduoj上超时了,但是在zoj上能过
for(i=;i<;i++)
{
xx=x+mov[i][];
yy=y+mov[i][];
if(can(xx,yy))
{
step++;
map[xx][yy]='X';
DFS(xx,yy);
step--;
map[xx][yy]='.';
}
}
}
int main()
{
int i,j;
while(scanf("%d%d%d",&m,&n,&t),m||n||t)
{
getchar();//吸收回车符
for(i=;i<m;i++)
{
for(j=;j<n;j++)
{
scanf("%c",&map[i][j]);
if(map[i][j]=='S')
{
bx=i;
by=j;
}
if(map[i][j]=='D')
{
ex=i;
ey=j;
}
}
getchar();
}
flag=;
step=;
int best=abs(ex-bx)+abs(ey-by);
if((best+t)&)//首先判断一下,如果最短路径和要走的步数奇偶性不同,就直接输出NO
{
printf("NO\n");
continue;
}
map[bx][by]='X';
DFS(bx,by);//深搜
if(flag)
printf("YES\n");
else
printf("NO\n");
}
return ;
}

下面看下修剪前后的时间差距

 
第三个是没剪枝的时候
第二个是没加满足情况就回溯那一句,在上面提到过
第一个是最后修剪成功
 
前两个在杭电都是超时的!
 
不懂得可以在下面提问,一定会尽快回复大家!谢谢
 
 

Tempter of the Bone--hdu1010--zoj2110的更多相关文章

  1. ZOJ2110 HDU1010 搜索 Tempter of the Bone

    传送门:Tempter of the Bone 大意是给一个矩阵,叫你是否可以在给定的可走路径上不重复地走,在最后一秒走到终点. 我用了两个剪枝,且称其为简直001和剪枝002,事实证明001不要都可 ...

  2. hdu1010 Tempter of the Bone —— dfs+奇偶性剪枝

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 Tempter of the Bone Time Limit: 2000/1000 MS (Ja ...

  3. ZOJ 2110 Tempter of the Bone(条件迷宫DFS,HDU1010)

    题意  一仅仅狗要逃离迷宫  能够往上下左右4个方向走  每走一步耗时1s  每一个格子仅仅能走一次且迷宫的门仅仅在t时刻打开一次  问狗是否有可能逃离这个迷宫 直接DFS  直道找到满足条件的路径 ...

  4. Hdu1010 Tempter of the Bone(DFS+剪枝) 2016-05-06 09:12 432人阅读 评论(0) 收藏

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

  5. HDU1010:Tempter of the Bone(dfs+剪枝)

    http://acm.hdu.edu.cn/showproblem.php?pid=1010   //题目链接 http://ycool.com/post/ymsvd2s//一个很好理解剪枝思想的博客 ...

  6. hdu1010 Tempter of the Bone(深搜+剪枝问题)

    Tempter of the Bone Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission( ...

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

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

  8. 【HDU - 1010】Tempter of the Bone(dfs+剪枝)

    Tempter of the Bone 直接上中文了 Descriptions: 暑假的时候,小明和朋友去迷宫中寻宝.然而,当他拿到宝贝时,迷宫开始剧烈震动,他感到地面正在下沉,他们意识到这是一个陷阱 ...

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

  10. ZOJ 2110 Tempter of the Bone

    Tempter of the Bone Time Limit: 2 Seconds      Memory Limit: 65536 KB The doggie found a bone in an ...

随机推荐

  1. Pausing Coyote HTTP/1.1 on http-8080

    一般情况下我看到8080便认为是端口占用的问题,其实不是,但是在任务管理器中并没有找到javaw.exe,只有javaservice.exe, 只有重启tomcat了,蓝后就好了......

  2. Oracle AWR

    http://www.linuxidc.com/Linux/2011-10/44563.htm http://t.askmaclean.com/thread-3227-1-1.html http:// ...

  3. BZOJ1106: [POI2007]立方体大作战tet

    1106: [POI2007]立方体大作战tet Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 419  Solved: 302[Submit][St ...

  4. 【转】关于Adapter的The content of the adapter has changed问题分析 关于Adapter的The content of the adapter has changed问题分析

    原文网址:http://www.cnblogs.com/monodin/p/3874147.html 1.问题描述 1 07-28 17:22:02.162: E/AndroidRuntime(167 ...

  5. C++基础回顾2(函数, 指针和引用)

    接着回顾函数.指针和应用. 函数 1.多维数组作为形参时,第一维的大小可以省略(也可以不省略),但是其他维的大小必须指定.比如二维数组形参,int array[3][]不正确,int arry[][1 ...

  6. socket pro

    /etc/exports/tmp目录共享为任何人可以共享并可以进行读写操作 /tmp *(rw,no_root_squash) /home/test 192.168.1.*(rw) *(ro) /et ...

  7. 如何备份及恢复Linux文件权限

    你可能听说或碰到过这样的事情:一个系统管理员菜鸟不小心输入"chmod -R 777 /"从而导致了巨大的悲剧,使得整个系统遭到了严重的破坏.在日常管理中,我们有许多工具可以用来备 ...

  8. JVM运行时内存结构

    原文转载自:http://my.oschina.net/sunchp/blog/369707 1.JVM内存模型 JVM运行时内存=共享内存区+线程内存区 1).共享内存区 共享内存区=持久带+堆 持 ...

  9. 第23讲 UI_布局 之相对布局

    第23讲 UI_布局 之相对布局 .RelativeLayout(相对布局): RelativeLayout(相对布局)是指组件的位置总是相对兄弟组件.父容器来决定的(相对位置),如某个组件的左边右边 ...

  10. Query语句对系统性能的影响

    需求: 取出某个group(假设id为1)下的用户编号id,用户昵称(nick_name),并按照加入组的时间(user_group.gmt_create)来进行倒序排列,取出前20个 解决方案一: ...