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. python登陆教务管理系统

    想试着模拟登陆一些网站,这次先拿学校的教务管理系统练练手,写一下登陆的流程. 1.我们登陆的url:http://222.195.8.201,但我们所填的密码不是提交到这个页面上去,检查一下页面代码 ...

  2. IOS开发教程之put上传文件的服务器的配置及实例分享-备用

    感谢大神分享 1,HTTP常见的方法 GET 获取指定资源 POST 2M 向指定资源提交数据进行处理请求,在RESTful风格中用于新增资源 HEAD 获取指定资源头部信息PUT 替换指定资源(不支 ...

  3. 这样就算会了PHP么?-7

    循环之类的例子 <script language="javascript"> function calculate(a, b) { return a * b; } do ...

  4. LeetCode_sqrt(x)

    class Solution { public: int sqrt(int x) { // Start typing your C/C++ solution below // DO NOT write ...

  5. 智能卡 ATR解析

    如果终端不支持IC卡支持的其它传输协议以及传输参数值,IC卡应该有能力用基本ATR定义的模式和终端进行交互. 终端如果无法满足IC卡回送ATR中定义的传输模式,将发送一个热复位信号,或将IC卡置为静止 ...

  6. Configuring HTTP and HTTPS

    Configuring HTTP and HTTPS .NET Framework (current version)   Other Versions   WCF services and clie ...

  7. 在docker以FPM-PHP运行php,慢日志导致的BUG分析

    问题描述: 最近将IOS书城容器化,切换流量后.正常的业务测试了一般,都没发现问题.线上的错误监控系统也没有报警,以为迁移工作又告一段落了,暗暗的松了一口气.紧接着,报警邮件来了,查看发现是一个苹果支 ...

  8. 段错误bug的调试

    我们在用C/C++语言写程序的时侯,内存管理的绝大部分工作都是需要我们来做的.实际上,内存管理是一个比较繁琐的工作,无论你多高明,经验多丰富,难 免会在此处犯些小错误,而通常这些错误又是那么的浅显而易 ...

  9. 移动web开发研究

    1.jQuery Mobile jQuery Mobile框架能够帮助你快速开发出支持多种移动设备的Mobile应用用户界面.jQuery Mobile最新版本是1.4.0,默认主题采用扁平化设计风格 ...

  10. Quartz定时调度CronTrigger时间配置格式说明与实例

    1.   CronTrigger时间格式配置说明 CronTrigger配置格式: 格式: [秒] [分] [小时] [日] [月] [周] [年] 序号 说明 是否必填 允许填写的值 允许的通配符 ...