本文链接:http://i.cnblogs.com/EditPosts.aspx?postid=5398734

题意:

  输入一个 N * M的迷宫,这个迷宫里'S'代表小狗的位置,'X'代表陷阱,‘D’代表门,‘.’代表可行走的地方,小狗每次可以选择往周围的四个方向行走,问这个小狗能否正好T步找到门。

思路:

  利用回溯 + 剪枝,这道题剪枝特别重要。

剪枝一:

可以把图看成这样:

1 0 1 0 1
0 1 0 1 0
1 0 1 0 1
0 1 0 1 0
1 0 1 0 1

则假设从点 a(i + j,横纵坐标之和) 走到点 b(i + j) ,如果 a 和 b 的奇偶性相同,那么从 a 到 b 必须是偶数步.如果 a  和 b 的奇偶性不同则走过的步数必须是奇数步。所以 当 (a + b + T)为奇数时一定不能恰好到达。

剪枝二:

如果已经找到答案就没有要继续求解。

剪枝三:

T大于从S到D的最长路径,小于从S到D的最短路径,则无解。

代码:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
using namespace std; const int MAXN = ;
char Gra[MAXN][MAXN];
int stepX[] = {, , , -};
int stepY[] = {-, , , };
int N, M, T;
int cnt;
int beginX, beginY, endX, endY;
int OK; int check(int x, int y)
{
if(Gra[x][y] == 'O') return ; // 走到了已经走过的路
if(Gra[x][y] == '*') return ; //走出了边界
if(Gra[x][y] == 'X') return ; //走到了墙
if(cnt > T) return ; //在此时走的步数已经大于总步数则
return ;
} void backtrack(int x, int y)
{
if(x == endX && y == endY ) //到达终点
{
if(cnt == T) //找到了答案
OK = ;
}
else
{
for(int i = ; i < ; i++) //在这点一共有四种选择(状态)
{
int tx = x + stepX[i];
int ty = y + stepY[i];
if(check(tx, ty)) //检查所选择的状态是否合理
{
++cnt; //记录走过的步数
Gra[tx][ty] = 'O'; //标记走过的地方
if(OK) return; //尤为重要的剪枝 ,如果找到答案,就不用继续递归,
backtrack(tx, ty);
Gra[tx][ty] = '.'; //恢复现场
--cnt; }
}
}
} int main()
{
//freopen("in.txt","r", stdin);
while(~scanf("%d%d%d",&N, &M, &T) && N)
{
getchar();
beginX = beginY = endX = endY = ;
memset(Gra, '*', sizeof(Gra));
for(int i = ; i <= N; i++)
{
for(int j = ; j <= M; j++)
{
scanf("%c",&Gra[i][j]);
if(Gra[i][j] == 'S')
beginX = i, beginY = j;
if(Gra[i][j] == 'D')
endX = i, endY = j;
}
getchar();
}
OK = ;
if( (T > M * N) || ( T < (abs(beginX - endX) + abs(beginY - endY)) ) || ( (beginX + beginY + endX + endY + T) & ))//剪枝二、三
{
printf("NO\n"); continue;
}
cnt = ;
Gra[beginX][beginY] = 'O';
backtrack(beginX, beginY);
if(OK) printf("YES\n");
else printf("NO\n");
}
return ;
}

HDU1010 Tempter of the Bone(回溯 + 剪枝)的更多相关文章

  1. HDU1010:Tempter of the Bone(dfs+剪枝)

    http://acm.hdu.edu.cn/showproblem.php?pid=1010   //题目链接 http://ycool.com/post/ymsvd2s//一个很好理解剪枝思想的博客 ...

  2. TZOJ 1221 Tempter of the Bone(回溯+剪枝)

    描述 The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked i ...

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

  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奇偶剪枝(t时刻恰好到达)】

    Tempter of the Bone Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  6. hdu1010 Tempter of the Bone(深搜+剪枝问题)

    Tempter of the Bone Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission( ...

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

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

  8. HDU1010 --- Tempter of the Bone(dfs+剪枝)

    小明做了一个很久很久的梦,醒来后他竟发现自己和朋友在一个摇摇欲坠的大棋盘上,他们必须得想尽一切办法逃离这里.经过长时间的打探,小明发现,自己所在的棋盘格子上有个机关,上面写着“你只有一次机会,出发后t ...

  9. HDU1010 Tempter of the Bone

    解题思路:相当经典的一题,回溯,具体细节处理见代码. #include<cstdio> #include<cstring> #include<algorithm> ...

随机推荐

  1. 嗯,ACM按照这个一步一步来。

        转一个搞ACM需要的掌握的算法.   要注意,ACM的竞赛性强,因此自己应该和自己的实际应用联系起来. 适合自己的才是好的,有的人不适合搞算法,喜欢系统架构,因此不要看到别人什么就眼红, 发挥 ...

  2. python中subprocess.Popen执行命令并持续获取返回值

    先举一个Android查询连接设备的命令来看看Python中subprocess.Popen怎么样的写法.用到的命令为 adb devices. import subprocess order='ad ...

  3. maven常用命令 与语法

    pom.xml 中个元素的意义 groupId 规定了这个项目属于哪个组,或者公司之类的 artifactId 定义了当前maven项目在组中唯一的ID version 版本号 常用命令 mvn co ...

  4. 最小化安装Linux的常用配置整理

    基于安全性考虑,将服务器进行最小化安装,毕竟软件包越少,漏洞越少,相对来说就约安全,但是最小化安装会给运维带来一些问题和不便,下面是我总结的,常见的一些配置和工具的安装,仅供各位大神参考,如有新的id ...

  5. Python网络编程(weekly summary1)

    网络的目的是什么?     用于信息传输.接受  能把各个点.面.体的信息链接到一起 实现资源的共享 OSI模型:     应用层:提供程序服务     表示层:数据加密.优化.压缩     会话层: ...

  6. Python全栈工程师(编码)

    ParisGabriel       Python 入门基础   补充: 主流3操作大系统 Windows: Winxp   Win7 Win8 Win10 Unix: Solaris(SUN) IO ...

  7. python pyinstaller 打包程序报错解决

    python打包exe,各种入坑 一.安装PyInstaller 1.安装pywin32 pip命令安装:pip install pywin32(推荐) 2.安装Pyinstaller pip命令安装 ...

  8. bayes学习笔记

    贝叶斯(BAYES)判别思想是根据先验概率求出后验概率,并依据后验概率分布作出统计推断.所谓先验概率,就是用概率来描述人们事先对所研究的对象的认识的程度:所谓后验概率,就是根据具体资料.先验概率.特定 ...

  9. B-Tree索引和Hash索引的区别

    Hash 索引结构的特殊性,其检索效率非常高,索引的检索可以一次定位,不像B-Tree 索引需要从根节点到枝节点,最后才能访问到页节点这样多次的IO访问,所以 Hash 索引的查询效率要远高于 B-T ...

  10. 【bzoj3866】The Romantic Hero dp

    题目描述 给你n个数,从中选出两个不相交非空集合S和T,使得S中的每一个元素都在T集合的前面,并且S集合中的所有数的亦或等于T集合中的所有数的与,求方案数 mod 10^9+7. 输入 The fir ...