现在的位置: 首页 > 综合 > 正文

hdu 1010 Tempter of the Bone

2018年05月02日 ⁄ 综合 ⁄ 共 3259字 ⁄ 字号 评论关闭
文章目录

Tempter of the Bone

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 13   Accepted Submission(s) : 4

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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

/*题解:
DFS+奇偶剪枝,写了很久,也错了很长时间,原来声明变量也不是随便声明的,一开始
全部声明为全局变量,结果CE了,改了改才AC。还有就是要使用C++头文件<iostream>,不然也CE。 
*/

//奇偶剪枝 
dis = abs(ex-sx)+abs(ey-sy)为此问题类中任意情况下,起点到终点的最短步数
结论:若 t-[abs(ex-sx)+abs(ey-sy)] 结果为非偶数(奇数),则无法在t步恰好到达 

链接:http://baike.baidu.com/view/7789287.htm

// =====================================================================================
// 
//       Filename:  Tempter of the Bone.cpp
//    Description:  Tempter of the Bone
//      Algorithm:  DFS 
//         Status: 	RunTime:78ms 	RunMemory:340KB 
//        Version:  Dev-C++ 5.5.3 
//        Created:  2014/9/6 16:26 
//       Revision:  none
//       Compiler:  GUN C++
//         Author:  Tip of the finger melody, 1466989448@qq.com
//        Company:  none
//
// =====================================================================================
#include<cstdio>
#include<cmath>
#include<iostream>
using namespace std; 
int ex,ey,ok,vis[10][10],dir[4][2]={0,1,0,-1,1,0,-1,0};
int N,M,T;
char map[10][10];
void DFS(int x,int y,int t)
{
	if(map[x][y]=='D'&&T==t)//递归边界 
	{  
		ok = 1;
		return;
	}
	if(t>=T||ok) return;  //步数大于时间,不成立 
	int dis = abs(x-ex)+abs(y-ey);
	if((dis+T-t)%2||dis>T-t) return;  //奇偶剪枝 
	vis[x][y] = 1;
	int a,b,i;
	for(i=0; i<4; i++)
	{
		a=x+dir[i][0];
		b=y+dir[i][1];
		if(a>=0&&b>=0&&a<N&&b<M&&map[a][b]!='X'&&!vis[a][b])
		{
			//控制边界、路的状态、不走回头路 
			DFS(a,b,t+1);
		}
	}
	vis[x][y]=0;//回溯为0 
}
int main()
{
	int i,j,count,sx,sy;
	while(scanf("%d %d %d",&N,&M,&T)&&(N||M||T))
	{
		for(i=0,count=1; i<N; i++)
		{
			scanf("%s",map[i]);
			for(j=0; j<M; j++)
			{
				if(map[i][j]=='S')
				{
					sx=i,sy=j;
					map[i][j]='X';
				}
				if(map[i][j]=='D')
				{
					ex=i,ey=j;
				}
				if(map[i][j]=='.')
					count++;
			}
		}
		int dis = abs(sx-ex)+abs(sy-ey);
		if(count<T||(dis+T)%2)//可走步数小于时间 奇偶剪枝 
		{ 
			printf("NO\n");
			continue;
		}
		ok = 0;
		DFS(sx,sy,0);
		if(ok) printf("YES\n");
		else	printf("NO\n");
	}
	return 0;
}

关于剪枝:
剪枝分为奇偶剪枝和路径剪枝,搜索题一般离不开剪枝。
奇偶剪枝: 
例如5X5的图:
0 1 0 1 0 
1 0 1 0 1
0 1 0 1 0
1 0 1 0 1
0 1 0 1 0
如果起点是0,终点是0,显然要经过偶数步,依次类推,奇偶相同的偶数步,奇偶不同的奇数步。
判断是否能从起点到终点,仅需判断t+abs(ex-sx)+abs(ey-sy)的奇偶性就行。
路径剪枝:
若墙的数量为wall,n*n-wall<T,可走步数小于时间,即不可能走到终点。 
 
 

抱歉!评论已关闭.