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
#include
#include
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

对于这道题,也是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 时间内在按照原路返回】

你可能感兴趣的:(搜索)