Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 135529    Accepted Submission(s): 36393

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

题意:

一个n*m的矩阵,老鼠的起点在矩阵中的'S'上,终点在矩阵中的'D',其中'X'是墙,老鼠不能通过,'.'是路但是只能通过一次,过了一次之后就不能再走这个地方了,终点D在第K秒是打开,这就要求老鼠能够在第K秒是正好到达D点,如果不能就输出NO,可以的话就输出YES.

思路:

用dfs解决,但是如果剪枝没弄好会超时,新学到了一个奇偶剪枝。

奇偶剪枝:从a到b的路径大小永远是 dis=abs(ax-bx)+abs(ay-by)+偶数 。所以以此可以先一步判断

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<math.h>
#include<iostream>
#include<algorithm>
#define INF 0x3f3f3f3f
#define N 10
using namespace std;
int m,n,t,flag;
char map[N][N];
int vis[N][N],step,to[4][2]={0,1,1,0,-1,0,0,-1}; void dfs(int x,int y){
if(flag) return;
if(map[x][y]=='D' && step==t){
flag=1;
return;
}
if(step>=t) return; for(int i=0;i<4;i++){
int fx=x+to[i][0],fy=y+to[i][1];
if(fx>=1 && fx<=n && fy>=1 && fy<=m && vis[fx][fy]==0 && map[fx][fy]!='X'){
step+=1;
vis[fx][fy]=1;
dfs(fx,fy);
step-=1;
vis[fx][fy]=0;
}
}
return;
} int main(){
int sx,sy,ex,ey,i,j;
while(~scanf("%d%d%d",&n,&m,&t) && n+m+t){
for(i=1;i<=n;i++){
scanf("%s",map[i]+1);
for(j=1;j<=m;j++){
if(map[i][j]=='S'){
sx=i,sy=j;
}
if(map[i][j]=='D'){
ex=i,ey=j;
}
}
}
int dis=abs(sx-ex)+abs(sy-ey);
if((dis%2)!=(t%2)){    //奇偶剪枝
printf("NO\n");
continue;
}
memset(vis,0,sizeof(vis));
step=0;
flag=0;
vis[sx][sy]=1;
dfs(sx,sy);
if(flag) printf("YES\n");
else printf("NO\n");
}
return 0;
}

Tempter of the Bone(dfs+奇偶剪枝)题解的更多相关文章

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

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

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

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

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

  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 Tempter of the Bone (奇偶剪枝)

    学习链接:http://www.ihypo.net/1554.html https://www.slyar.com/blog/depth-first-search-even-odd-pruning.h ...

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

  7. hdu1010Tempter of the Bone(dfs+奇偶剪枝)

    题目链接:pid=1010">点击打开链接 题目描写叙述:给定一个迷宫,给一个起点和一个终点.问是否能恰好经过T步到达终点?每一个格子不能反复走 解题思路:dfs+剪枝 剪枝1:奇偶剪 ...

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

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

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

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

  10. hdoj--1010<dfs+奇偶剪枝>

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目描述:在n*m的矩阵中,有一起点和终点,中间有墙,给出起点终点和墙,并给出步数,在该步数情况 ...

随机推荐

  1. 建立一个更高级别的查询 API:正确使用Django ORM 的方式(转)

    add by zhj: 本文作者是DabApps公司的技术主管,作者认为在view中直接使用Django提供的ORM查询方法是不好的,我对此并不赞同,可能作者 写这篇文章是给Django的初学者看,所 ...

  2. Service 的 onStartCommand(Intent, int, int) 返回值

    (1)START_NOT_STICKY If the system kills the service after onStartCommand() returns, do not recreate ...

  3. ifconfig 查看网卡信息

    [root@linux-node- sss]# ifconfig eno16777736: flags=<UP,BROADCAST,RUNNING,MULTICAST> mtu inet ...

  4. visual studio code 的必装推荐插件plugin, vscode, vsc

    An Old Hope Theme     (theme, 推荐,且推荐它的 classic theme,安装后在颜色选项里选择,该插件的定制见文末) Cobalt2     (theme) Drac ...

  5. 5分钟带你入门vuex(vue状态管理)

    如果你之前使用过vue.js,你一定知道在vue中各个组件之间传值的痛苦,在vue中我们可以使用vuex来保存我们需要管理的状态值,值一旦被修改,所有引用该值的地方就会自动更新,那么接下来我们就来学习 ...

  6. Python 使用ctypes调用 C 函数

    在python中通过ctypes可以直接调用c的函数,非常简单易用 下面就一步一步解释用法吧,以Linux为例讲解. 1, 首先确定你的python支持不支持ctypes python2.7以后cty ...

  7. 十四、springboot全局处理异常(@ControllerAdvice + @ExceptionHandler)

    1.@ControllerAdvice 1.场景一 在构建RestFul的今天,我们一般会限定好返回数据的格式比如: { "code": 0, "data": ...

  8. PAT 1076 Forwards on Weibo[BFS][一般]

    1076 Forwards on Weibo (30)(30 分) Weibo is known as the Chinese version of Twitter. One user on Weib ...

  9. 深入理解python之一——python3对象的一些特性

    文章使用markdown写的,编辑的时候行间距还可以,显示的时候行间距好小,我也没办法. 首先,需要明白的是python是一种解释性语言标准,他可以由c.java或者其他语言来实现.在python的标 ...

  10. 算法---数组总结篇2——找丢失的数,找最大最小,前k大,第k小的数

    一.如何找出数组中丢失的数 题目描述:给定一个由n-1个整数组成的未排序的数组序列,其原始都是1到n中的不同的整数,请写出一个寻找数组序列中缺失整数的线性时间算法 方法1:累加求和 时间复杂度是O(N ...