HDU1010 Tempter of the Bone(回溯 + 剪枝)
本文链接: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(回溯 + 剪枝)的更多相关文章
- HDU1010:Tempter of the Bone(dfs+剪枝)
http://acm.hdu.edu.cn/showproblem.php?pid=1010 //题目链接 http://ycool.com/post/ymsvd2s//一个很好理解剪枝思想的博客 ...
- 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 ...
- 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 ...
- 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 ...
- HDU1010 Tempter of the Bone【小狗是否能逃生----DFS奇偶剪枝(t时刻恰好到达)】
Tempter of the Bone Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- hdu1010 Tempter of the Bone(深搜+剪枝问题)
Tempter of the Bone Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission( ...
- hdu 1010 Tempter of the Bone 奇偶剪枝
如果所给的时间(步数) t 小于最短步数path,那么一定走不到. 若满足t>path.但是如果能在恰好 t 步的时候,走到出口处.那么(t-path)必须是二的倍数. 关于第二种方案的解释 ...
- HDU1010 --- Tempter of the Bone(dfs+剪枝)
小明做了一个很久很久的梦,醒来后他竟发现自己和朋友在一个摇摇欲坠的大棋盘上,他们必须得想尽一切办法逃离这里.经过长时间的打探,小明发现,自己所在的棋盘格子上有个机关,上面写着“你只有一次机会,出发后t ...
- HDU1010 Tempter of the Bone
解题思路:相当经典的一题,回溯,具体细节处理见代码. #include<cstdio> #include<cstring> #include<algorithm> ...
随机推荐
- 《Cracking the Coding Interview》——第16章:线程与锁——题目4
2014-04-27 20:06 题目:设计一个类,只有在不产生死锁的时候才分配资源. 解法:不太清楚这个题是要分配何种资源,以何种形式?所以没能动手写个可运行的代码,只是闲扯了几句理论分析. 代码: ...
- USACO Section2.3 Zero Sum 解题报告 【icedream61】
zerosum解题报告----------------------------------------------------------------------------------------- ...
- hnust py road
问题 C: Py Road 时间限制: 1 Sec 内存限制: 128 MB提交: 125 解决: 34[提交][状态][讨论版] 题目描述 Life is short,you need Pyth ...
- Elasticsearch相关度评分_score
相关度评分 _score 的目的 是为了将当前查询的结果进行排序,比较不同查询结果的相关度评分没有太大意义. _score的计算方式 score(q,d) = # score(q,d) 是文档 d 与 ...
- STL之算法使用简介
accumlate : iterator 对标志的序列中的元素之和,加到一个由 init 指定的初始值上.重载的版本不再做加法,而是传进来的二元操作符被应用到元素上. adjacent_differ ...
- ASP.NET 抓取网页内容
(转)ASP.NET 抓取网页内容 ASP.NET 抓取网页内容-文字 ASP.NET 中抓取网页内容是非常方便的,而其中更是解决了 ASP 中困扰我们的编码问题. 需要三个类:WebRequest. ...
- Application_Start事件中用Timer做一个循环任务
protected void Application_Start(object sender, EventArgs e) { System.Timers.Timer timer = new Syste ...
- PB数据窗口中的几种状态及应用
数据窗口的状态主要有以下几种: 1)New! 2)NewModified! 3)DataModified! 4)NotModified! 数据窗口可以利用这些状态标志判断数据是否被修改过. 记录和字段 ...
- 浅析Kerberos原理,及其应用和管理
文章作者:luxianghao 文章来源:http://www.cnblogs.com/luxianghao/p/5269739.html 转载请注明,谢谢合作. 免责声明:文章内容仅代表个人观点, ...
- 【bzoj2732】[HNOI2012]射箭 二分+半平面交
题目描述 给出二维平面上n个与y轴平行的线段,求最大的k,使得存在一条形如$y=ax^2+bx(a<0,b>0)$的抛物线与前k条线段均有公共点 输入 输入文件第一行是一个正整数N,表示一 ...