HD1010_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 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


这是一道经典搜索题,常规方法是深搜,当然要加上必要的剪枝条件

主要的剪枝条件有:

1、剩余可走区域小于时间

2、超时

3、越界

4、奇偶性剪等


下面主要说说奇偶性剪枝

若有一迷宫,将迷宫的每一个位置有0或1表示(x+y为偶数时 为0 否则为1):

0    1    0    1    0

1    0    1    0    1

0    1    0    1    0

1    0    1    0    1


从图中我们可以很清晰的看出:任意一个位置周围相邻的必然是与本身值相反的值,也就是说,要想走到与本身值相同的点必然要走偶数步;

同理,要想走到与本身相异的值的点必然要走奇数步;

所以,当两个位置的奇偶性相同时(同为0或同为1)若时间为奇数  则必然无法到达;当两个位置的奇偶性不同时(一个为1,

另一个为0)若时间为偶数也必不能到达。

是不是感觉很神奇,这个世界瞬时明朗了许多,搜索路径加上减去不必要的路径,最终达到出口。

另外本题注意输入输出的时候最好用C++语言,格式c用以在换行时候出错。

最后令我意外的是第一次发现用C++竟然比C和C++混用要快一点,涨见识了。

点击打开题目链接

下面是代码:


#include<iostream>

using namespace std;

char maze[8][8];
int direction[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
bool flag;
int si,sj,ei,ej,n,m,t;

void dfs(int starti, int startj, int time){
	
	if(flag==true) return ;
	if(maze[starti][startj]=='X')	return ;
	if(starti<0 || starti>n-1 || startj<0 || startj>m-1 )	//这点一定要注意是 n-1和 m-1 超时了很多次
		return;
	if(starti==ei && startj==ej && time==t){
		flag=true;
		return ;
	}
	
	if(time>t)	return ;
	for(int i=0; i<4; ++i){
		maze[starti][startj]='X';
		dfs(starti+direction[i][0],startj+direction[i][1],time+1);
		maze[starti][startj]='.';
		if(flag==true)	return;
	}	
}

int main(){
	int block;
	while(cin>>n>>m>>t,m+n+t/*scanf("%d%d%d",&n,&m,&t)!=EOF,n+m+t*/){
		block=0;
		flag=false;
		for(int i=0; i<n; ++i){
			for(int j=0; j<m; ++j){
				cin>>maze[i][j];
				//scanf("%c",maze[i][j]);
				if(maze[i][j]=='X'){
					block++;
					continue;
				}
				if(maze[i][j]=='S'){
					si=i;
					sj=j;
					continue;
				}
				if(maze[i][j]=='D')
				{
					ei=i;
					ej=j;
				}
			}
		}
		if(t>(n*m-block)){<span style="white-space:pre">	</span>//当前可用时间大于可走布数
			//printf("NO\n");
			cout<<"NO\n";
			continue;
		}
		if((si+sj+ei+ej+t)%2==1){<span style="white-space:pre">	</span>//奇偶剪枝
		//	printf("NO\n");
			cout<<"NO\n";
			continue;
		}
		dfs(si,sj,0);
		if(flag==true)
			cout<<"YES\n";
		//	printf("YES\n");
		else
			//printf("NO\n");
			cout<<"NO\n";
	}
	return 0;
}



你可能感兴趣的:(搜索,深搜+剪枝)