http://acm.hdu.edu.cn/showproblem.php?pid=1010

题意:就是给出了一个迷宫,小狗必须经过指定的步数到达出口,并且每个格子只能走一次。

首先先来介绍一下奇偶性剪枝:

在这道题目中,如果使用剪枝的话,可以节省不少的时间。

在这道题目中,每次dfs循环时都可以判断一下小狗当前位置与终点所相差的步数,如果不为偶数的话,说明到达不了终点,就可以退出这个循环,不必继续dfs了。

在这道题目中,由于每个格子只能经过一次,所以经过一次后,可以把该点位置改为‘X’,然后我wa了好久,后来明白每次dfs循环后得把这个点的位置重新改回‘.’。

 #include<iostream>
#include<cstring>
#include<string>
using namespace std; char map[][];
int d[][] = { { , }, { -, }, { , }, { , - } };
int flag,n,m,t,dx,dy; void dfs(int x,int y,int time)
{
if (x<||x>n||y<||y>m ||flag||time>t) return; //出界
if (time == t && x==dx && y==dy)
{
flag = ;
return;
}
int s1 = x - dx;
int s2 = y - dy;
int ans = t - time - abs(s1) - abs(s2); //剪枝,如果当前剩余的所要求的步数减去小狗
if (ans<||ans%) return; //当前位置与终点的步数不为偶数的话,则结束
for (int i = ; i < ; i++)
{
if (map[x + d[i][]][y + d[i][]] != 'X')
{
map[x + d[i][]][y + d[i][]] = 'X';
dfs(x + d[i][], y + d[i][], time+);
map[x + d[i][]][y + d[i][]] = '.'; //这里必须把该点的值还原回来,不然影响后续的dfs
}
}
return;
} int main()
{
int x,y,wall;
while (cin >> n >> m >> t, n && m && t)
{
if (!m || !n || !t)
{
cout << "NO" << endl;
continue;
}
flag = ;
wall = ;
for (int i = ; i <= n;i++)
for (int j = ; j <= m; j++)
{
cin >> map[i][j];
if (map[i][j] == 'S')
{
x = i;
y = j;
}
if (map[i][j] == 'D')
{
dx = i;
dy = j;
}
if (map[i][j] == 'X')
wall++;
}
if (n*m - wall <t) //剪枝,如果所有点减去墙小于指定步数,那肯定是不行的
{
cout << "NO" << endl;
continue;
}
map[x][y] = 'X';
dfs(x,y,);
if (flag == ) cout << "YES" << endl;
else cout << "NO" << endl;
}
return ;
}

HDU 1010 Tempter of the Bone(深度+剪枝)的更多相关文章

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

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

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

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

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

  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(深搜+奇偶剪枝)

    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(dfs)

    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 分类: 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 ...

随机推荐

  1. TortoiseSVN文件夹及文件图标不显示解决方法

              由于自己的电脑是win7(64位)的,系统安装TortoiseSVN之后,其他的功能都能正常的使用,但是就是文件夹或文件夹的左下角就是不显示图标,这个问题前一段时间就遇到了(那个时 ...

  2. Loadrunner11安装和破解方法

    公司很多项目都在做性能测试,打算把性能测试学习下.(不懂还可以问问公司大神,这么好的机会不要错过了O(∩_∩)O哈哈~)用了二周实践看了性能测试方面一些基本术语和概念,一直都还没自己动手实践,光看基本 ...

  3. C++ 之 新式转型操作符

    四种新式转型: const_cast.dynamic_cast.reinterpret_cast.static_cast!! 1.const_cast  :  去除常量性 2.dynamic_cast ...

  4. Android Activity中获取当前焦点的控件,自动化输入EditText

    获取焦点的view对象 View view=getWindow().getDecorView().findFocus(); 如果是EditText if(view instanceof EditTex ...

  5. 几种通过JDBC操作数据库的方法,以及返回数据的处理

    1.SQL TO String :只返回一个查询结果 例如查询某条记录的总数 rs = stmt.executeQuery(replacedCommand);             if (rs ! ...

  6. JDBC操作步骤及数据库连接操作

    http://blog.csdn.net/joywy/article/details/7731305

  7. css3--布局正六边形

    怎样布局正六边形?-->如果不能直接布局,就只能采用图形的组合.-->既然是正六边形,则: -->AB=2分之根号3乘2倍的边长,也就是对于矩形ABCD来说,AB是BD的根号3倍(也 ...

  8. VMware10 安装centos6.7 设置NAT模式

    最近刚开始学Linux运维.我看的书是<跟阿铭学Linux>,视频教程里面使用NAT模式手动分配IP可以成功ping通网关,但是我照着视频一步一步操作却一直不成功,不知道是什么原因,昨天弄 ...

  9. 时间星期农历js

    <script> var CalendarData=new Array(20); var madd=new Array(12); var TheDate=new Date(); var n ...

  10. oracle length and lengthb

    LENGTH──返回以字符为单位的字符串长度. LENGTHB──返回以字节为单位的字符串长度,它和类型定义中的长度是一个概念,比如你定义的varchar2(10)中的10.在不同的数据库,因为字符集 ...