很典型的dfs题,但是涉及到很多的剪枝 。

奇偶剪枝:
是数据结构的搜索中,剪枝的一种特殊小技巧。
现假设起点为(sx,sy),终点为(ex,ey),给定t步恰好走到终点,
s        
|        
|        
|        
+ e
 
如图所示(“|”竖走,“—”横走,“+”转弯),易证abs(ex-sx)+abs(ey-sy)为此问题类中任意情况下,起点到终点的最短步数,记做step,此处step1=8;
  
s  
  +  
| +      
|        
+ e
 
如图,为一般情况下非最短路径的任意走法举例,step2=14;
step2-step1=6,偏移路径为6,偶数(易证);
故,若t-[abs(ex-sx)+abs(ey-sy)]结果为非偶数(奇数),则无法在t步恰好到达;
返回,false;
反之亦反。
奇偶路径的大概意思就是:从初始位置能到达目标位置的任一路径长度  -   初始位置到目标位置的最短长度=偶数
#include"iostream"
#include"stdio.h"
#include"algorithm"
#include"queue"
#include"string.h"
#include"string"
#define mx 105
using namespace std;
char maze[mx][mx];
int n,m,T,sx,sy,ex,ey;
int dir[][]={{,},{-,},{,},{,-}};
bool flag;
bool judge(int x,int y)
{
if(x>=&&x<n&&y>=&&y<m&&maze[x][y]=='.') return true;
return false;
}
void dfs(int x,int y,int t)
{
if(t>T||flag) return;
if(t==T&&x==ex&&y==ey) {flag=true;return;}
int temp=abs(x-ex)+abs(y-ey);//当前位置到目标位置的最短路径
temp=T-t-temp;
if(temp%) return;//奇偶剪枝,不加这个一直tle也是醉了 。
for(int i=;i<;i++)
{
int dx=x+dir[i][];
int dy=y+dir[i][];
if(judge(dx,dy))
{
maze[dx][dy]='X';
dfs(dx,dy,t+);
maze[dx][dy]='.';
}
}
}
int main()
{ int i,j,blocks;
while(cin>>n>>m>>T,n&&m&&T)
{
flag=false;blocks=;
for(i=;i<n;i++)
{
for(j=;j<m;j++)
{
cin>>maze[i][j];
if(maze[i][j]=='S')
{
sx=i;sy=j;maze[i][j]='X';
}
else if(maze[i][j]=='D')
{
ex=i;ey=j;maze[i][j]='.';
}
else if(maze[i][j]=='X') blocks++;
}
}
if(n*m-blocks->=T)//如果给定的时间数比能走的格子数还要大的话,就肯定不满足条件了
dfs(sx,sy,);
if(flag) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return ;
}

hdu Tempter of the Bone的更多相关文章

  1. HDOJ/HDU Tempter of the Bone(深搜+奇偶性剪枝)

    Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, ...

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

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

  3. HDU 1010 Tempter of the Bone --- DFS

    HDU 1010 题目大意:给定你起点S,和终点D,X为墙不可走,问你是否能在 T 时刻恰好到达终点D. 参考: 奇偶剪枝 奇偶剪枝简单解释: 在一个只能往X.Y方向走的方格上,从起点到终点的最短步数 ...

  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 奇偶剪枝

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

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

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

  8. Hdu 1010 Tempter of the Bone 分类: Translation Mode 2014-08-04 16:11 82人阅读 评论(0) 收藏

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

  9. hdu 1010 Tempter of the Bone 深搜+剪枝

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

随机推荐

  1. How to choose the number of topics/partitions in a Kafka cluster?

    This is a common question asked by many Kafka users. The goal of this post is to explain a few impor ...

  2. 【转】【异常处理】Incorrect string value: '\xF0\x90\x8D\x83...' for column... Emoji表情字符过滤的Java实现

    http://blog.csdn.net/shootyou/article/details/44852639 Emoji表情字符现在在APP已经广泛支持了.但是MySQL的UTF8编码对Emoji字符 ...

  3. telnet时显示:允许更多到 telnet 服务器的连接。请稍候再试

    telnet时显示:允许更多到 telnet 服务器的连接.请稍候再试    解决办法: windows自带telnet服务器默认的最大连接数为2,要想修改该设置,可以在命令行键入tlntadmn c ...

  4. JMeter常用字符串相关函数

    JMeter的惯用函数使用-字符串相关 主要的函数如下:1.将字符串转为大写或小写: ${__lowercase(Hello,)}  ${__uppercase(Hello,)}2.生成字符串:  _ ...

  5. css之overflow:细探之下有意想不到的结果

    overflow 是一个非常常用的 CSS 属性,一般来说会认为很简单,其实细究之后就会发现他还有很多小特性或者说意想不到的结果: 下面就介绍下(在浏览器环境下)关于 overflow 的小总结. 哪 ...

  6. 看Ue4角色代码——跳跃与实现二段跳

    看了一下终于发现了跳跃的关键代码 bool UCharacterMovementComponent::DoJump(bool bReplayingMoves) { if ( CharacterOwne ...

  7. HDU5812 Distance(枚举 + 分解因子)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5812 Description In number theory, a prime is a ...

  8. OpenCV 第二课 认识图像的存储结构

    OpenCV 第二课 认识图像的存储结构 Mat Mat 类包含两部分,矩阵头和矩阵体.矩阵头包含矩阵的大小,存储方式和矩阵体存储空间的指针.因此,Mat中矩阵头的大小是固定的,矩阵体大小是不定的. ...

  9. BZOJ4471 : 随机数生成器Ⅱ

    \[\begin{eqnarray*}x_i&=&x_{i-1}+x_{i-2}\\x_i^2&=&x_{i-2}^2+x_{i-1}^2+2x_{i-2}x_{i-1 ...

  10. hadoop2.2.0 + hbase 0.94 + hive 0.12 配置记录

    一开始用hadoop2.2.0 + hbase 0.96 + hive 0.12 ,基本全部都配好了.只有在hive中查询hbase的表出错.以直报如下错误: java.io.IOException: ...