点击打开链接

Tempter of the Bone

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

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

ZJCPC2004

Recommend

JGShining   |   We have carefully selected several similar problems for you:  1016 1242 1072 1312 1026

题意:

输入一个n*m的迷宫,T为可以在迷宫中生存的最大时间,。S为起点,D为终点。.为路,X为障碍,每个格子只能踩一次,且只能维持一秒,然后该块地板就会塌陷。所以你必须每秒走一步,且到D点时,所用时间为T。

题解:

从起点开始向上下左右四个方向搜索,遇到路“."则继续向下搜索,遇到障碍”X“则结束搜索;当走到D点时且T=0时,输出YES,否则为NO;这样朴素的搜索会超时,所以需要剪枝

剪枝1:

如果剩余时间T小于当前点到终点的最短距离,则这条路径不可能按时到大,停止向下搜索

最短路径=abs(ex-x)+abs(ey-y);

剪枝2:

初始化n*m的01矩阵
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
下标从1开始,a【i】【j】=(i+j)% 2
在地图上,权值相同的两点距离为偶数
权值不同的两点距离为奇数
若(a【x】【y】+a【ex】【ey】)为偶数,
如果剩余时间t为偶数,则可能到达
如果剩余时间t为奇数,则无法到达

若(a【x】【y】+a【ex】【ey】)为奇数,
如果剩余时间t为偶数,则无法到达
如果剩余时间t为奇数,则可能到达

综上,
若(a【x】【y】+a【ex】【ey】+t)为偶数,
则可能到达
如果为奇数,则无法到达

#include<bits/stdc++.h>
using namespace std;
int sx,sy,ex,ey;
int n,m;
char mp[10][10];
int d[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
bool flag;
void dfs(int x,int y,int t)
{if(flag==1)    return;//若找到解,则不需要找其他的路径//剪枝if(t<abs(ex-x)+abs(ey-y))   return ;//如果剩余时间小于当前点到达终点的横纵坐标之和
/*if((t-abs(ex-x)+abs(ey-y))%2)   return;//若无障碍,则差值为0,否则绕道
*/
if((t+x+y+ex+ey)%2)  return;//剪枝2if(t==0){if(x==ex&&y==ey){flag=1;return;}else  return;}elsefor(int i=0;i<4;i++){int nx=x+d[i][0],ny=y+d[i][1];if(nx>0&&nx<=n&&ny>0&&ny<=m&&(mp[nx][ny]=='.'||mp[nx][ny]=='D')){mp[nx][ny]='X';//标记走过dfs(nx,ny,t-1);mp[nx][ny]='.';//回溯}}
}
int main()
{int t;char str[10];while(scanf("%d%d%d",&n,&m,&t)!=EOF){if(n==0&&m==0&&t==0)    break;for(int i=1;i<=n;i++){scanf("%s",str);for(int j=1;j<=m;j++){mp[i][j]=str[j-1];if(mp[i][j]=='S')sx=i,sy=j;else if(mp[i][j]=='D')ex=i,ey=j;}}flag=0;dfs(sx,sy,t);if(flag)    printf("YES\n");else printf("NO\n");}return 0;
}

HDU1010 Tempter of the Bone DFS+剪枝相关推荐

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

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

  2. HDU1010 Tempter of the Bone dfs(奇偶减枝)

    直接搜索会超时,需要减枝(奇偶减枝). #include<stdio.h> #include<string.h> int n,m,t; char map[7][7]; int ...

  3. hdu1010 Tempter of the Bone

    转载自:http://acm.hdu.edu.cn/forum/read.php?tid=6158 sample input: 4 4 5 S.X. ..X. ..XD .... 问题: (1): 在 ...

  4. (step4.3.1) hdu 1010(Tempter of the Bone——DFS)

    题目大意:输入三个整数N,M,T.在接下来的N行.M列会有一系列的字符.其中S表示起点,D表示终点. .表示路 . X表示墙...问狗能有在T秒时到达D.如果能输出YES, 否则输出NO 解题思路:D ...

  5. HDU1010 Tempter of the Bone(DFS奇偶剪枝)

    传送门 Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. Howe ...

  6. HDU 1010 Tempter of the Bone DFS(奇偶剪枝优化)

    需要剪枝否则会超时,然后就是基本的深搜了 #include<cstdio> #include<stdio.h> #include<cstdlib> #include ...

  7. 【hdoj_1010】Tempter of the Bone(迷宫+剪枝)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意:给出一个迷宫(含起点和终点),要求找出一条路径,这条路径的长度必须为某个规定的长度. 在做 ...

  8. HDU 1010 Tempter of the Bone heuristic 剪枝法

    本题就是考剪枝法了. 应该说是比较高级的应用了.因为要使用heuristic(经验)剪枝法.要总结出这个经验规律来,不容易.我说这是高级的应用也因为网上太多解题报告都没有分析好这题,给出的程序也很慢, ...

  9. hdu1010 Tempter of the Bone

    题目意思:   一只吉娃娃去迷宫捡骨头, 捡到骨头后发现是一个陷阱, 然后就想逃出迷宫:迷宫是N*M 规格的, 迷宫只有一道门且 只在第 T 秒钟开一会儿(少于1秒) 也就是说只在[ t, t+1) ...

最新文章

  1. forward 和redirect
  2. The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone
  3. redis 判断存在性_实战 | springboot+redis+拦截器 实现接口幂等性校验
  4. 矩阵拼接_TEC无缝拼接矩阵切换器(处理器)的技术
  5. 如何查找,修复和避免C#.NET中内存泄漏的8个最佳实践
  6. 无服务器-构建现代应用程序的新方法
  7. 前端学习(1035):bootstrap-js插件1
  8. 信息学奥赛一本通(1141:删除单词后缀)
  9. 删除数组中的某一个元素
  10. 达摩院 2020 预测:量子霸权指日可待!
  11. C# http监听之Nancy.net
  12. 软件测试方法进行调优,性能测试调优过程
  13. 文档管理系统解决方案
  14. 对话夏琳·查布利斯:Primer.AI机器学习工程师是怎样炼成的?
  15. java rrd 读取_RRD插入值的计算方式
  16. appium2.0+ 单点触控和多点触控新的解决方案
  17. [OGRE]基础教程来三发:来谈一谈摄像机吧
  18. ios屏幕的旋转,Device Orientation
  19. deepin 切换大黄蜂显卡驱动
  20. SIP 协议的系统构架

热门文章

  1. Standard Template Library标准模板库专项复习总结(一)
  2. 蓝桥杯 能量项链 (区间dp)
  3. Markdown的常用语法
  4. 论SqlServer中char、varchar、nvarcahr和MySQL中char、varcahr的区别
  5. 1071. Speech Patterns (25)
  6. BZOJ2958 序列染色
  7. BZOJ3239 Discrete Logging
  8. 二叉树节点数据结构-练习 5 二叉树的建立 遍历
  9. 文件上传服务器经常超时,解决Tomcat文件上传超时问题.
  10. python逻辑表达式3+45and_Python 简明教程 --- 5,Python 表达式与运算符