本文链接: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. Pascal游戏 大富翁MUD

    大富翁MUD Pascal源码 Chaobs改编自百度网友作品 此源码非Chaobs原创,但转载时请仍注明出处. 估计90后以后就没有孩子知道MUD了. program wxtw; uses crt; ...

  2. 一个初学者的辛酸路程-jQuery

    前言: 主要概要: 1.HTML+CSS补充 2.DOM事件 3.jQuery示例 内容概要: 1.布局 代码如下 <!DOCTYPE html> <html lang=" ...

  3. java初学2

    1.数组操作类Arrays与System public static void arraycopy(Object src, int srcPos, Object dest,int destPos,in ...

  4. TW实习日记:第四天

    第四天 早上第一件事就是和组长说前一天的需求的事,简而言之就是两个导航栏不属于一个标签内,自定义导航栏属于<body>下的<header>,微信顶部的则是<head> ...

  5. c#中获得MD5字符串方法

    在用户登录的过程中,我们会遇到要查询对比用户名密码的是否存在或者是否正确,但是数据库中存放的是通过MD5加密的字符串,所有我们可以先把用户输入的用户名或者是密码先转为DM5字符串再跟数据库查出的MD5 ...

  6. MYSQL 简单的建库操作代码

    一.查询所有数据库 代码:show databases; 成功后如下图: 二.建立一个数据库 代码:create database test3: 成功后如下图: 三.连接数据库 代码:use test ...

  7. JVM垃圾回收机制GC

    1. 垃圾回收的意义 在C++中,对象所占的内存在程序结束运行之前一直被占用,在明确释放之前不能分配给其它对象:而在Java中,当没有对象引用指向原先分配给某个对象的内存时,该内存便成为垃圾.JVM的 ...

  8. vue.$refs 的用法

    官网给出的解释是: 被用来给元素或子组件注册引用信息.引用信息将会注册在父组件的 $refs 对象上. 1.如果在普通的 DOM 元素上使用,引用指向的就是 DOM 元素; 2.如果用在子组件上,引用 ...

  9. 2017 多校6 String

    多校6 String(ac自动机) 题意: 给一本有\(n\)个单词的字典 \(q\)个查询 \(pref_i,suff_i\) 查询字典里有多少单词前缀匹配\(pref_i\),后缀同时匹配\(su ...

  10. 有趣的数(number)

    有趣的数(number) 题目描述 这些天 nodgd 在研究一类有趣的数.定义函数 f(n) f(n) f(n) 表示 n n n 在十进制表示下的数字之和.如果一个正整数 n n n 满足 f(n ...