解题思路:相当经典的一题,回溯,具体细节处理见代码。

 #include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn = ;
int vis[maxn][maxn], dir[][] = {, , -, , , , , -};
char mapp[maxn][maxn];
int n, m, t, flag, cnt, si, sj, di, dj; void DFS(int x, int y, int cnt)
{
if(x == di && y == dj) //如果走到终点
{
if(cnt == t) flag = ; //并且步数恰好等于时间,门正好打开。
return ; //不过是否走出去,都必须返回
}
if(cnt >= t) return ; //超过时间再继续走已经没有意义了。
if(mapp[x][y] == 'X') return ; //X的地方是不能走的。
vis[x][y] = ; //标记为已经走过
for(int i = ; i < ; i++) //四个方向搜索
{
int xx = x + dir[i][];
int yy = y + dir[i][]; //初始化时边界之外的地方都标记为X,所以此处包括边界的判断。
if(mapp[xx][yy] == 'X' || vis[xx][yy]) continue;
vis[xx][yy] = ; //标记为已走过
DFS(xx, yy, cnt + ); //搜索
vis[xx][yy] = ; //回溯
if(flag) return ; //这步至关重要,否则会TLE
}
return ;
} int main()
{
while(~scanf("%d %d %d", &n, &m, &t) && (n || m || t))
{
memset(vis, , sizeof(vis)); //全部标记为没有走过
memset(mapp, 'X', sizeof(mapp)); //边界之外也标记为X
for(int i = ; i <= n; i++)
{
for(int j = ; j <= m; j++)
{
scanf(" %c", &mapp[i][j]);
if(mapp[i][j] == 'S') si = i, sj = j;
if(mapp[i][j] == 'D') di = i, dj = j;
}
} //非常经典的剪枝,否者会TLE
//如果走到该点所用的最少步数都超过t,是不可能走到的。
//后面一个条件是奇偶判断的问题,需要好好思考
if(abs(si-di) + abs(sj-dj) > t || (si+sj+di+dj+t) % == )
{
printf("NO\n");
continue;
} flag = , cnt = ;
DFS(si, sj, );
if(flag) printf("YES\n"); //如果flag==1,则表明走出来了。
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 Tempter of the Bone Time Limit: 2000/1000 MS (Ja ...

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

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

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

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

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

  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(回溯 + 剪枝)

    本文链接:http://i.cnblogs.com/EditPosts.aspx?postid=5398734 题意: 输入一个 N * M的迷宫,这个迷宫里'S'代表小狗的位置,'X'代表陷阱,‘D ...

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

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

  8. ZOJ 2110 Tempter of the Bone(条件迷宫DFS,HDU1010)

    题意  一仅仅狗要逃离迷宫  能够往上下左右4个方向走  每走一步耗时1s  每一个格子仅仅能走一次且迷宫的门仅仅在t时刻打开一次  问狗是否有可能逃离这个迷宫 直接DFS  直道找到满足条件的路径 ...

  9. ZOJ2110 HDU1010 搜索 Tempter of the Bone

    传送门:Tempter of the Bone 大意是给一个矩阵,叫你是否可以在给定的可走路径上不重复地走,在最后一秒走到终点. 我用了两个剪枝,且称其为简直001和剪枝002,事实证明001不要都可 ...

随机推荐

  1. 加载gif动态图的三种方式

    准备:本地图片资源,GifView

  2. POJ 2181

    #include <iostream> #include <cstdio> #include <cmath> #define MAXN 150005 #includ ...

  3. SharePoint文档库文件大小限制(win2008+II7)问题

    我们在用SharePoint存储文档时,用户要上传五十多MB到站点上,结果受到上传大小限制.在管理中心里做了修改,增加了上载大小限制.可是用户在上传的时候,提示复制一个或多个文件失败(win2003) ...

  4. Codeforces Round #337 (Div. 2) B. Vika and Squares 水题

    B. Vika and Squares   Vika has n jars with paints of distinct colors. All the jars are numbered from ...

  5. BZOJ 2661: [BeiJing wc2012]连连看 费用流

    2661: [BeiJing wc2012]连连看 Description 凡是考智商的题里面总会有这么一种消除游戏.不过现在面对的这关连连看可不是QQ游戏里那种考眼力的游戏.我们的规则是,给出一个闭 ...

  6. Java集合框架(三)

     Map Map集合:该集合存储键值对,一对一对的往里存,而且要保证键的唯一性. Map |------HashTable:底层是哈希表数据结构,不可以存入null键null值.该集合是线程同步的.J ...

  7. js之数组常见的方法

    主要介绍数组的一些常用的方法,方法多了,就容易混淆,结果就是方法用错,甚至不会用: 一.数组的定义: 1.字面量/直接量: var arr = [1, 2, 'js', 'java']; 2.通过内部 ...

  8. lintcode :同构字符串

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  9. I,P,B帧和PTS,DTS的关系

    http://www.cnblogs.com/qingquan/archive/2011/07/27/2118967.html 基本概念: I frame :帧内编码帧 又称intra picture ...

  10. 计算CRC校验值(CRC16和CRC32)(网络传输检验)

    CRC有非常多的模式,我没有全部都做,目前支持 CRC16-Modbus CRC16-X25 CRC32 使用方法 auto data = QByteArray::fromHex( "01 ...