hdoj-1010-Tempter of the Bone【深搜+剪枝】

Tempter of the Bone

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

Total Submission(s): 90097 Accepted Submission(s): 24482

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:
1242 1072 1312 1026 1240

#include<stdio.h>
#include<string.h>
#include<math.h>
int dx,dy,m,n;
char map[10][10];
bool visit[10][10];
int ok;
void dfs(int i,int j,int t){
    if(ok||t<0) return;
	if(i==dx&&j==dy){
	    if(t==0)
	      ok=1;
		return;
	}
    int tt=t-abs(i-dx)-abs(j-dy);
    if(tt<0||tt%2==1) return;
    if(i<0||i>=n||j<0||j>=m||visit[i][j]||map[i][j]=='X') return;

	visit[i][j]=1;
	dfs(i,j-1,t-1);
	dfs(i,j+1,t-1);
	dfs(i-1,j,t-1);
	dfs(i+1,j,t-1);
	visit[i][j]=0;
}
int main(){
	int t;
	while(~scanf("%d%d%d",&n,&m,&t)&&(n||m||t)){
	    memset(visit,0,sizeof(visit));
	    int sx,sy,z=0;
	    char ch;
		for(int i=0;i<n;++i){
			for(int j=0;j<m;++j){
				while(scanf("%c",&ch)){
					if(ch=='.'||ch=='X'||ch=='S'||ch=='D')
					break;
				}
				map[i][j]=ch;
				if(map[i][j]=='S') sx=i,sy=j;
				if(map[i][j]=='D') dx=i,dy=j;
			}
		}
		ok=0;
		dfs(sx,sy,t);
		if(!ok) printf("NO\n");
		else printf("YES\n");
	}
	return 0;
}

对于这道题,也是WA醉了,刚开始理解错题意,以为是在 t 时间内出来就可以了,没想到是要恰好在第t 时间找到门,后来就一直超时,再后来用了奇偶剪枝后一直WA,调试了一天,提交了 n 多遍,居然问题出在 输入,输入的n,m,t中可能还有很多空格,实在太坑了

******************下面说说奇偶剪枝******************************

从点(x0,y0)到点(x1,y1)的最短移动距离 dis= abs(x0-x1)+ abs(y0-y1);也就是说在水平方向和竖直方向要移动的最短距离和,在此题中 要在余下的有限时间 t内移动的目标位置,就要判断: t-dis 是奇数还是偶数,【因为假设t-dis=1,那么时间就要多1,而在一个时间单位内无论向哪个方向移动,都会与目标位置相对距离错一个,相反如果为偶数的话,就可以认为:在多余的(t-dis)/2
时间内往某方向移动,接下来在剩余的(t-dsi)/2 时间内在按照原路返回】

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-06 00:53:34

hdoj-1010-Tempter of the Bone【深搜+剪枝】的相关文章

hdu 1010 Tempter of the Bone 深搜+剪枝

题意:在一个坐标内,给定起点和终点,问能否恰好在t时刻到达终点. 以前很少写搜索题,所以看到这个题,就按照普通的深搜写了一下,交上去超时了.后来在网上搜了一下才知道,要剪枝才行.可是,我以前从没写过剪枝,不知道怎么剪,就按照别人的思路往下想.看懂以后,我对剪枝的理解是:对于一些没有必要继续搜索的路径,不再往下深搜,提前返回到上一层.花了半天时间调试代码,终于AC了. 奇偶剪枝:根据题目,doggie必须在第t秒到达门口.也就是需要走t-1步.设doggie开始的位置为(sx,sy),目标位置为(

HDU - 1010 Tempter of the Bone 深搜模板题(DPS)解题报告

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 88587    Accepted Submission(s): 24116 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

HDU 1010 Tempter of the Bone(深搜)

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 86037    Accepted Submission(s): 23462 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

HDU 1010 Temper of the bone(深搜+剪枝)

Tempter of the Bone 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

HDOJ/HDU Tempter of the Bone(深搜+奇偶性剪枝)

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 despe

hdu 1010 Tempter of the Bone (DFS+剪枝)

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 68206    Accepted Submission(s): 18719 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

hdoj 1010 Tempter of the Bone【dfs查找能否在规定步数时从起点到达终点】【奇偶剪枝】

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 89317    Accepted Submission(s): 24279 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

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

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 75141    Accepted Submission(s): 20531 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

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

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 82702    Accepted Submission(s): 22531 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

hdu 1010 Tempter of the Bone DFS+奇偶剪枝,入门题

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 78390    Accepted Submission(s): 21395 Problem Description The doggie found a bone in an ancient maze, which fascinated him a