题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010

Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 131057    Accepted Submission(s): 35308

Problem Description
The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone was a trap, and he tried desperately to get out of this maze.

The maze was a rectangle with sizes N by M. There was a door in the maze. At the beginning, the door was closed and it would open at the T-th second for a short period of time (less than 1 second). Therefore the doggie had to arrive at the door on exactly the T-th second. In every second, he could move one block to one of the upper, lower, left and right neighboring blocks. Once he entered a block, the ground of this block would start to sink and disappear in the next second. He could not stay at one block for more than one second, nor could he move into a visited block. Can the poor doggie survive? Please help him.

 
Input
The input consists of multiple test cases. The first line of each test case contains three integers N, M, and T (1 < N, M < 7; 0 < T < 50), which denote the sizes of the maze and the time at which the door will open, respectively. The next N lines give the maze layout, with each line containing M characters. A character is one of the following:

'X': a block of wall, which the doggie cannot enter; 
'S': the start point of the doggie; 
'D': the Door; or
'.': an empty block.

The input is terminated with three 0's. This test case is not to be processed.

 
Output
For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.
 
Sample Input
4 4 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0
 
Sample Output
NO
YES
 
Author
ZHANG, Zheng
 
Source
 
Recommend
JGShining
 

代码如下:

 #include<stdio.h>//hdu1010 dfs+奇偶性剪枝
#include<stdlib.h> char map[][];
int n,m,t, wall, si, sj, di, dj;
int d[][] = {{, }, {, -}, {, }, {-, }}; int dfs(int i, int j, int step)
{
if(step == t)//如果最后一秒
{ //判断是否到达door
if(i == di && j == dj) return ;
else return ;
} if (i == di && j == dj )//如果到达door
{
//判断是否最后一秒
if (step == t) return ;
else return ;
} //奇偶性剪枝
int temp = t-step-abs(i - di) + abs(j - dj);
if( temp% )
return ; for (int k = ; k<; k++)
{
int x = i + d[k][];
int y = j + d[k][];
if (x>= && x<=n && y>= && y<=m && map[x][y] != 'X')
{
map[x][y] = 'X';
if(dfs(x, y, step + )) return ;
map[x][y] = '.';//回溯出口,记得复原(可能把'D'变成'.',但已经用didj保存其位置,所以没关系)
}
}
return ;
} int main()
{
while(scanf("%d%d%d",&n,&m,&t) &&m &&n &&t)
{
wall = ;
for (int i = ; i<=n; i++)
{
scanf("%s",map[i]+);
for (int j = ; j<=m; j++)
{
//用scanf(%c) 就出问题?
if (map[i][j] == 'S') { si = i; sj = j; }
if (map[i][j] == 'D') { di = i; dj = j;}
if (map[i][j] == 'X') { wall++; }
}
} map[si][sj] = 'X';
if (n * m - wall- < t)
{ //初始判断剩余的空格(记得减去初始位置)是否够走
puts("NO");
continue;
} if (dfs(si, sj, ))
puts("YES");
else
puts("NO");
}
return ;
}

hdu1010 Tempter of the Bone —— dfs+奇偶性剪枝的更多相关文章

  1. hdu - 1010 Tempter of the Bone (dfs+奇偶性剪枝) && hdu-1015 Safecracker(简单搜索)

    http://acm.hdu.edu.cn/showproblem.php?pid=1010 这题就是问能不能在t时刻走到门口,不能用bfs的原因大概是可能不一定是最短路路径吧. 但是这题要过除了细心 ...

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

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

  3. hdu.1010.Tempter of the Bone(dfs+奇偶剪枝)

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  4. Tempter of the Bone 搜索---奇偶性剪枝

    Tempter of the Bone Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) ...

  5. M - Tempter of the Bone(DFS,奇偶剪枝)

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

  6. Tempter of the Bone(dfs奇偶剪枝)

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  7. HDU 1010 Tempter of the Bone(DFS+奇偶剪枝)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意: 输入 n m t,生成 n*m 矩阵,矩阵元素由 ‘.’ 'S' 'D' 'X' 四 ...

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

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

  9. hdu 1010 Tempter of the Bone (奇偶性剪枝)

    题意:有一副二维地图'S'为起点,'D'为终点,'.'是可以行走的,'X'是不能行走的.问能否只走T步从S走到D? 题解:最容易想到的就是DFS暴力搜索,,但是会超时...=_=... 所以,,要有其 ...

随机推荐

  1. tomcat7.0.55配置单向和双向HTTPS连接

    HTTPS配置中分为单向连接和双向连接,单向连接只需要服务器安装证书,客户端不需要,双向连接需要服务器和客户端都安装证书 下面的配置都没有用CA签名来配置,都不能用于生产环境,实际配置中是需要CA的, ...

  2. sed理论讲解、实战

    1.Sed是操作.过滤和转换文本内容的强大工具,常用功能有增删改查.过滤.取行. options(常用参数): -n:使用安静(silent)模式,在一般 sed 的用法中,所有来自 STDIN 的数 ...

  3. CDOJ 92 Journey LCA乱搞

    原题链接:http://acm.uestc.edu.cn/#/problem/show/92 题意: 给你一棵树,然后在树上连接一条边.现在有若干次询问,每次问你两个点(u,v)之间的距离在加那条边之 ...

  4. H264码率设置

    转帖 http://blog.csdn.net/jefry_xdz/article/details/8299901 一.什么是视频码率? 视频码率是视频数据(视频色彩量.亮度量.像素量)每秒输出的位数 ...

  5. 关于 Bellman-Ford 与 Floyd 算法的一点感想

    在四种常用的最短路算法 Dijkstra, SPFA, floyd, Bellman-Ford 中, Dijks 和 SPFA 的使用较为普遍, 对大多数人来说, 也较为熟悉. 然而, floyd 与 ...

  6. tomcat访问(access)日志配置、记录Post请求参数

    tomcat访问(access)日志配置.记录Post请求参数 一.配置与说明 tomcat访问日志格式配置,在config/server.xml里Host标签下加上 <Valve classN ...

  7. ADO如何记录SQL日志

    ADO如何记录SQL日志 procedure TfrmDM.ADOConnection1WillExecute(Connection: TADOConnection; var CommandText: ...

  8. Direct2D教程(一)Direct2D已经来了,谁是GDI的终结者?

    什么是Direct2D 一言以蔽之,就是Windows 7平台上的一个2D图形API,可以提供高性能,高质量的2D渲染.大多数人对Direct2D可能都比较陌生,以至于我之前在论坛上提到这个词的时候, ...

  9. EasyHook库系列使用教程之四钩子的启动与停止

    此文的产生花费了大量时间对EasyHook进行深入了解同一时候參考了大量文档 先来简单比較一下EasyHook与Detour钩取后程序流程 Detours:钩取API函数后.产生两个地址,一个地址相应 ...

  10. css3 - target

    通过CSS3伪元素target,我们可以实现拉风琴 源码 <!DOCTYPE HTML> <html lang="en-US"> <head> ...