1. 如果所给的时间(步数) t 小于最短步数path,那么一定走不到。
  2. 若满足t>path。但是如果能在恰好 t 步的时候,走到出口处。那么(t-path)必须是二的倍数。

关于第二种方案的解释:

这种方案学名为“奇偶剪枝”。我们已知了最短的步数就是直角三角形的两条直角边,实际上的路径却不一定非要沿着这两条边走的。仔细看看只要是移动方向一直是右、下,那么走到的时候总步数也一定是path的。然而由于墙的存在或许我们不可能一直右、下的走下去。为了避开墙,我们可能会向左走,向上走等等。但为了到达目的地,你在最短步数的基础上,如果向右走了一步,那么某一时候也必须再向左走一步来弥补。所以(t-path)一定要是2的倍数。

C - Tempter of the Bone

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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
 
#include<iostream>
#include<stdio.h>
using namespace std;
bool vis[][];
struct Node
{
int x,y;
int step;
} node[][];
int n,m,t;
char ma[][];
int dx[]= {,,,-};
int dy[]= {,-,,};
bool dfs(int x,int y)
{
if(ma[x][y]=='S')
{
node[x][y].step=;
}
if(ma[x][y]=='D')
{
if(node[x][y].step==t) return true;
else return false;
}
for(int i=; i<; i++)
{
int nx=dx[i]+x;
int ny=dy[i]+y;
if((!vis[nx][ny])&&(nx>=&&ny>=)&&(nx<n&&ny<m)&&(ma[nx][ny]!='X'))
{
vis[nx][ny]=true;
node[nx][ny].step=node[x][y].step+;
if(node[nx][ny].step<=t)
if(dfs(nx,ny)) return true;
vis[nx][ny]=false;
node[nx][ny].step--;
}
}
return false;
}
int main()
{ while((scanf("%d%d%d",&n,&m,&t))&&(n+m+t)!=)
{
int x,y;
int ex,ey;
for(int i=; i<; i++)
for(int j=; j<; j++)
{
vis[i][j]=false;
node[i][j].step=;
}
for(int i=; i<n; i++)
{
scanf("%s",ma[i]);
}
for(int i=; i<n; i++)
for(int j=; j<m; j++)
if(ma[i][j]=='S')
{
x=i;
y=j;
}
else if(ma[i][j]=='D')
{
ex=i;
ey=j;
}
if(abs(ex-x)+abs(ey-y)>t||(t-abs(ex-x)-abs(ey-y))%==)
{
printf("NO\n");
continue;
}
if(dfs(x,y))
printf("YES\n");
else printf("NO\n");
}
return ;
}

hdu 1010 Tempter of the Bone 奇偶剪枝的更多相关文章

  1. HDU 1010 Tempter of the Bone --- DFS

    HDU 1010 题目大意:给定你起点S,和终点D,X为墙不可走,问你是否能在 T 时刻恰好到达终点D. 参考: 奇偶剪枝 奇偶剪枝简单解释: 在一个只能往X.Y方向走的方格上,从起点到终点的最短步数 ...

  2. hdu 1010(迷宫搜索,奇偶剪枝)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1010 Tempter of the Bone Time Limit: 2000/1000 MS (Ja ...

  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. HDU 1010 Tempter of the Bone(DFS+奇偶剪枝)

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

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

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

  7. hdu 1010 Tempter of the Bone(深搜+奇偶剪枝)

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

  8. HDU 1010 Tempter of the Bone (DFS+可行性奇偶剪枝)

    <题目链接> 题目大意:一个迷宫,给定一个起点和终点,以及一些障碍物,所有的点走过一次后就不能再走(该点会下陷).现在问你,是否能从起点在时间恰好为t的时候走到终点. 解题分析:本题恰好要 ...

  9. hdu 1010 Tempter of the Bone 深搜+剪枝

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

随机推荐

  1. 小谈android/Linux rootkit(基于LKM)

    最近又学习了一下,感觉还有好多东西不知道,以后积累多一点再从新写一个. 在android上捣鼓了一下linux的内核rootkit,虽然中途遇到了无数坑,至今也没有完全写完,打算先好好啃一段时间lin ...

  2. java Iterable

    Iterable

  3. mysql语法语句

    将一个字段中的timestamp修改成可视化时间 update table set f1 = IF( LOCATE('-',f1)>0, f1, IFNULL(FROM_UNIXTIME(f1/ ...

  4. 随机取若干条记录的SQL语句

    原文:随机取若干条记录的SQL语句 MySql中随机提取数据库N条记录 select * from TableName order by rand() limit N   SQLServer中随机提取 ...

  5. linux的file指令

    显示文件的类型,用命令 file 可以使你知道某个文件究竟是ELF格式的可执行文件, 还是shell script文 件或是其他的什么格式 例如:#file startx 语 法:file [-beL ...

  6. 使用Chrome DevTools直接调试Node.js与JavaScript(并行)

    Good News: 现在我们可以用浏览器调试node.js了!!! 前提 Node.js 6.3+, 这个可上Node.js官网自行下载: Chrome 55+. 如果您本地的chrome升级到最新 ...

  7. Android-Adapter-View复用机制

    前言 相信Android开发者对ListView不会陌生,使用ListView需要设置相应的Adapter才能展示数据.Adapter到底是什么东西?让我们来一探究竟. Adapter p1.png ...

  8. Android反编译调试源码

    Android反编译调试源码 1. 反编译得到源码 直接在windows 命令行下输入命令java -jar apktool_2.0.0.jar d -d 小米运动_1.4.641_1058.apk ...

  9. git push后自动部署

    前提,服务器已经装好ssh,本地也已经将ssh 公钥传到服务器对应位置 先用 pbcopy < ~/.ssh/PRIVATE_KEY.pub 将公钥复制到剪贴板:通过 ssh USER@SERV ...

  10. swift初探(供objective c开发人员參考)

    6月初的wwdc苹果推出了一门新的开发语言swift.系统10.9.3以上安装xcode6 beta版就可以体验swift. 苹果公司做了尽可能多的努力让这门语言迅速成为一个工业级的有用编程语言,而不 ...