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): 90803    Accepted Submission(s): 24712


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
注 - 此题为:  HDOJ 1010 Tempter of the Bone  (DFS && 奇偶剪枝)

题意:   在一个n行m列的迷宫中,  每一步只能向上、下、左、右中任意方向走一格,  迷宫中有围墙的地方是无法到达的,而且,走过的不能再走。从起点 s 开始,能否刚好走 T步,到达  e 。

思路: DFS 深搜  +  奇偶减枝。    只用 深搜 ,超时,必须减枝

奇偶减枝:    可以把 map  看成这样:

   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

从 0 开始走一步 必然会到    1  ;  从 1  走一步 必然会到  0  。

因此:0 --> 1 或 1 --> 0   必然是 奇数步,

            0-->  0 或  1-->  1   必然是 偶数步。

所以:   当遇到从  0  走向  0   ,但是 要求 时间是 奇数   ,或者  从 1  走向  0   但是要求时间是  偶数   ,都是 不可能的。

已AC代码:

#include<cstdio>
#include<cstring>

int N,M,T,sx,sy,ex,ey;
char map[10][10];
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};

bool Judge_Ji_ou(int x)  // 判断奇偶性 
{
	if(x&1) // 奇数返回 0 
		return 0;
	return 1;
}

bool Judge_Ke_zou(int x,int y) // 判断是否可走 
{
	if(x<0||x>=N||y<0||y>=M) // 判断是否越界 
		return 0;
	if(map[x][y]=='X') // 判断是否可走 
		return 0;
	return 1;
}

bool DFS(int x,int y,int t)
{
	if(!Judge_Ji_ou(x+y+ex+ey+t)) // x+y+ex+ey+t 和为偶数 返回 0  
		return 0;
		
	if(x==ex&&y==ey) // 找到终点 
	{
		if(t==0)  // 刚好把给定要求步数走完,满足题意 返回 1  
			return 1;
		return 0;  // 否则返回 0 
	}
	
	if(t==0) // 还没走到终点, 给定要求步数就走完了,返回 0 
		return 0;
		
	for(int i=0;i<4;++i) // 四向搜索 
	{
		int nx=x+dx[i];
		int ny=y+dy[i];

		if(!Judge_Ke_zou(nx,ny)) // 这一步不可走 搜索下一步 
			continue;

		map[nx][ny]='X'; // 将已走的标记为不可走的'X' 
			
		if(DFS(nx,ny,t-1))  // 可走 步数 减 1 
			return 1; 
				
		map[nx][ny]='.';  // 回溯,取消标记,变回可走的 '.' 
	}
	return 0;
}

int main()
{
	int i,j,s;
	while(scanf("%d%d%d",&N,&M,&T),N,M,T)
	{
		for(i=0;i<N;++i)
			scanf("%s",map[i]);
		s=0; 
		for(i=0;i<N;++i)
		{
			for(j=0;j<M;++j)
			{
				if(map[i][j] == 'S') //  记录起点坐标 
					sx=i,sy=j;
				else if(map[i][j] == 'D') // 记录终点坐标 
					ex=i,ey=j;
				else if(map[i][j] == '.') // 记录可走的点 
					s++;
			}
		}
		map[sx][sy] = 'X'; // 起点 标记 为 不可走的 'X' 
		
		if(s < T-1) // 如果总可走点数 比 给定要求可走点数 少 2 ,不可能 
			printf("NO\n");
		else if( Judge_Ji_ou(sx+sy+ex+ey+T) && DFS(sx,sy,T) )  // 总共有 T 步,每走一步 T 减 1 
			printf("YES\n");  // sx+sy+ex+ey+T为偶数,DFS(sx,sy,T)返回 1 满足条件,输出 YES 
		else
			printf("NO\n"); // sx+sy+ex+ey+T为奇数,DFS(sx,sy,T) 返回 0 输出 NO 
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值