本文链接: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. 《Cracking the Coding Interview》——第6章:智力题——题目4

    2014-03-20 01:02 题目:无力描述的一道智力题,真是货真价实的智力题,让我充分怀疑自己智力的智力题.有兴趣的还是看书去吧. 解法:能把题目看懂,你就完成80%了,用反证法吧. 代码: / ...

  2. 【Search in Rotated Sorted Array II 】cpp

    题目: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would t ...

  3. 油田(DFS)

    //DFS:油田问题 #include <iostream> using namespace std; ][]; int n,m; //一个网格的8个方向 ][] = {{-,-},{-, ...

  4. unity生命周期

    1.静态构造函数 当程序集被加载的时候就被调用了,如果你的unity处于编辑状态时,此时你保存一个脚本(从而迫使重新编译),静态构造函数会立即被调用,因为unity加载了DLL.并且它将不会再次运行, ...

  5. 【转载】Unity3D研究院transform.parent = parent坐标就乱了

    昨天有朋友问我了一个问题,它将Hierarchy视图里的某个子节点下的GameObject拷贝到另外一个对象的子节点下面,他使用的方法就是 transform.parent = parent 但是拷贝 ...

  6. Linux查看端口被占用情形

    查看某端口的占用情况: lsof -i:<端口号> 例如:lsof -i:8080 netstat -apn|grep <端口号> 例如: netstat -apn | gre ...

  7. git使用及一些配置、问题

    安装https://git-for-windows.github.io/ 一.绑定用户名.邮件地址 git config --global user.name "Your Name" ...

  8. 哈希URAL 1941 - Scary Martian Word

    A - Scary Martian Word Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I ...

  9. PHP面向对象关键词static 、self

    知识点: 一.static可以修饰类内的属性或方法,被修饰的属性或方法在类外部,不能被实例化成对象访问,而是使用类本身进行访问,而静态的方法如果想使用静态的属性,则需要用self::这样的写法来访问静 ...

  10. gulp (转)

    “1. 我为什么使用grunt: 2. 我为何放弃grunt转投gulp: 3. 我为何放弃gulp与grunt,转投npm scripts: 4. 我为何放弃前端” —— 司徒正美 前端(段子)界的 ...