很典型的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. 学习linux内核时常碰到的汇编指令(1)

     转载:http://blog.sina.com.cn/s/blog_4be6adec01007xvg.html 80X86 汇编指令符号大全 +.-.*./∶算术运算符. &∶宏处理操作符. ...

  2. snakeyaml - Documentation.wiki

    SnakeYAML Documentation This documentation is very brief and incomplete. Feel free to fix or improve ...

  3. JavaWeb监听器详解

    1 JavaWeb监听器概述 在JavaWeb被监听的事件源为:ServletContext.HttpSession.ServletRequest,即三大域对象.有监听域对象"创建" ...

  4. thinkphp数据表操作恐怖事件。

    1当使用thinkphp的where(array())时,如果里面的字段在数据库是没有的,则默认这个条件为1,这时就可能出现大批修改记录问题.如修改所有用户的密码.特别要注意的是,这里的表字段是区分大 ...

  5. Linux常用命令之安装VMware10中安装CentOS 6.4

    笔者用过的Linux系统也就是现在主流的企业级linu系统RedHat跟CentOS,这边主要介绍下CentOS 6.4的安装 RedHat和CentOS差别不大,CentOS是一个基于RedHat  ...

  6. JAVA第三周作业

    一:枚举 package homework; public class EnumTest { public static void main(String[] args) { Size s=Size. ...

  7. SpringJMS解析3-监听器

    消息监听器容器是一个用于查看JMS目标等待消息到达的特殊bean,一旦消息到达它就可以获取到消息,并通过调用onMessage()方法将消息传递给一个MessageListener实现.Spring中 ...

  8. 《DSP using MATLAB》示例Example5.2

    代码: L = 5; N = 20; k = [-N/2:N/2]; % square wave parameters xn = [ones(1,L), zeros(1,N-L)]; % Sq wav ...

  9. D7控件\dw_cd_VirtualTreeview_v4.5.2\Demos\Advanced---TVirtualStringTree用法

    VST1: TVirtualStringTree; //按钮公用函数,根据不同 标签tag区分, Screen.Cursor := crHourGlass; //设置屏幕鼠标的形状为crhourGla ...

  10. 【bzoj2440】【bzoj3994】莫比乌斯反演学习

    哇..原来莫比乌斯代码这么短..顿时感觉逼格-- 写了这道题以后,才稍稍对莫比乌斯函数理解了一些 定理:和是定义在非负整数集合上的两个函数,并且满足条件,那么我们得到结论 在上面的公式中有一个函数,它 ...