hdu 1010 Tempter Of TheBone

Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 40763    Accepted Submission(s): 11029


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

题意:给出一个NxM的迷宫,并给出起点  终点  障碍  可走点,每个位置只能走一次不允许走多次,并且要求必须在第T步时到达终点,给出每组样例能否按要求到达终点。


题解:迷宫问题,DFS+奇偶剪枝,刚开始看数据量不是很大,直接DFS,测了测样例能过,测了下discuss的数据也都能过,交上去TLE,郁闷。然后就剪枝,想了一个如果当前剩余步数小于最短路径步数,那肯定是到不了的了,把这个剪枝加上去,提交,依然TLE,无语了,继续看看discuss,飘到一眼奇偶剪枝,不懂,看百科,大概是,所走的(非最短路径)路径 减去 最短路径必然要是偶数才可达。即(T-(abs(startx-endx)+abs(starty-endy)))%2!=0 肯定是不可达的直接输出NO,有了这项剪枝,交上去总算AC了。


代码:

/*
hdu:Tempter of the Bone
*/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<iostream>
#include<string>
char Maze[10+5][10+5];
int N=0,M=0,T=0;
int Second=0;
int Startx=-1,Starty=-1,Endx=-1,Endy=-1;
int flag=0;
int Time=0;
int Mins=0;
/*initialize the var*/
int InitVar()
{
	Second=0;
	Startx=-1;
	Starty=-1;
	Endx=-1;
	Endy=-1;
	flag=0;
	Time=0;
	return(0);
}
/*DFS the maze*/
int DFS(int x,int y)
{
	if(flag)
	{
		return(0);//if YES then quikly exit the DFS
	}
	if(Maze[x][y]!='X'&&x>=0&&x<=N-1&&y>=0&&y<=M-1)
	{
		if(Maze[x][y]=='.'||Maze[x][y]=='S')
		{
			if(Maze[x][y]=='.')
			{
				Time++;
			}
			Maze[x][y]='X';
			DFS(x-1,y);
			DFS(x,y-1);
			DFS(x+1,y);
			DFS(x,y+1);
			if(x!=Startx||y!=Starty)
			{
				Time--;
			}
			Maze[x][y]='.';
		}
		else
		{
			Time++;
			if(Time==T)
			{
				flag=1;
			}
			Time--;
		}
	}
	return(0);
}
/*for test*/
int test()
{
	return(0);
}
/*main process*/
int MainProc()
{
	while(scanf("%d%d%d",&N,&M,&T)!=EOF&&(N>0))
	{
		InitVar();
		int i=0,j=0;
		for(i=0;i<=N-1;i++)
		{
			scanf("%s",Maze[i]);
			if(Startx<0||Endx<0)
			{
				for(j=0;j<=M-1;j++)
				{
					if(Maze[i][j]=='S')
					{
						Startx=i;
						Starty=j;
					}
					if(Maze[i][j]=='D')
					{
						Endx=i;
						Endy=j;
					}
				}
			}
		}
		Mins=abs(Startx-Endx)+abs(Starty-Endy);
		if((T-Mins)%2!=0)//must have this parity pruning
		{
			flag=2;
		}
		DFS(Startx,Starty);
		if(flag==1)
		{
			printf("YES\n");
		}
		else
		{
			printf("NO\n");
		}
	}
	return(0);
}
int main()
{
	MainProc();
	return(0);
}



你可能感兴趣的:(hdu 1010 Tempter Of TheBone)