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. android studio使用中遇到的问题

    旧版和新版切换会报错(点击更正, 不影响程序使用) 2.debug正常, 打包签名程序时候报错 String index out of range: -82 java.lang.StringIndex ...

  2. leetcode日记 Combination sum IV

    题目: Given an integer array with all positive numbers and no duplicates, find the number of possible ...

  3. mac idea中 maven项目添加的时候没有java文件

    file --other setting --maven  选中第二项即可  apply下

  4. 非Animal呢?为何不写个万用类

    /*4.非Animal呢?为何不写个万用类 * 类Object是JAVA里多有类的源头/父类*/ import java.util.*; class Animalb{ String name; voi ...

  5. NSLOG打印不全的问题

    #ifdef DEBUG #define NSLog(FORMAT, ...) fprintf(stderr, "%s:%zd\t%s\n", [[[NSString string ...

  6. 编写可维护的JavaScript代码

    1.  1)for-in循环用来遍历对象属性.不仅遍历对象的实例属性,还遍历从原型继承来的属性. 所以最好使用hasOwnProperty()方法来过滤. 2) for in循环遍历出的对象属性不能保 ...

  7. php正则替换:

    1.要求: 对于: $str = '<p></p><p style="text-indent:241px"><strong>< ...

  8. css中文乱码与替换字符

    有时候,我们的css样式表中字体乱码,很诧异.百度谷歌是两个老师,有时jquery博客还上淘宝,那边有现成的代码,看看,发现里面定义全局字体是这样的font:12px/1.5 tahoma,arial ...

  9. 对Ajax的理解

    一.Ajax的工作原理: 1. Ajax的机制是:完成异步请求,实现页面的局部刷新. 2. 发送异步请求:通过xmlhttprequest方法. 3. 浏览器向服务器发送异步请求: 服务器接收处理请求 ...

  10. Web测试中常见分享问题

         Web测试中,由于开发通常指注重完成H5页面的逻辑功能,对各种系统.浏览器等考虑不周,同时Android端各类机型碎片化,容易产生兼容性问题,这其中以分享类型为最. 本文简单分析总结一些测试 ...