HDU1010 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): 51836    Accepted Submission(s): 13942


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


解题思路:该题有点障眼法,咋的一看是广度优先搜索搜索算法,最短时间的样子,结果就是,这道题目,有很多人WA了。其实不然,迷宫的门只有在T秒那一下打开,所以,要T秒那一下到达门处。也就是说,求的是满足条件的存在路径,并非最短路径,所以得用深度优先搜索算法。好吧,很多善良而又单纯的孩子想到这里,就开始改代码了(或干脆重写),我表示我就是这些善良又单纯的孩子之一。没猜错的话,你超时了,虽然迷宫最大限度只有7*7,但是,很耗时,数据量比较大。所以,还是乖乖剪枝吧。典型的奇偶剪枝,思路不难,我就不解说了。



#include<cstdio> #include<cstring> #include<algorithm> using namespace std; int n,m,t; int f; char map[8][8]; int flag[8][8]; int sx,sy,ex,ey; int dir[4][2]={1,0,0,1,-1,0,0,-1}; bool inmap(int x,int y) {     if(x>=0&&x<n&&y>=0&&y<m)         return true;     return false; } void dfs(int x,int y,int t) {     //printf("x=%d y=%d t=%d\n",x,y,t);     int nx,ny;     if(t<0||f)         return ;     else if(x==ex&&y==ey&&t==0) // 按时走到终点     { 	/*printf("\n"); 	for(int i=0;i<n;i++) 	{ 	for(int j=0;j<m;j++) 	{ 	printf("%d ",flag[i][j]); 	} 	printf("\n"); 	}         printf("\n");*/         f=1;         return ;     }     for(int i=0;i<4;i++)     {         nx=x+dir[i][0];         ny=y+dir[i][1];         //printf(" for  nx=%d ny=%d t=%d\n",nx,ny,t);         if((abs(ex-nx)+abs(ey-ny)-t-1)&1)   //对于当前点为起点(终点不变)的迷宫 			 			//距离之差减时间为基数,永远不能按时走到终点的迷宫{剪枝} 			 		{ 			//printf("nx=%d ny=%d t=%d\n",nx,ny,t);  			return ;  		}  		else if(inmap(nx,ny)&&!flag[nx][ny])  		{ //printf("&&! x=%d y=%d t=%d\n",x,y,t);  			flag[nx][ny]=t-1; dfs(nx,ny,t-1);  			flag[nx][ny]=0; 		} 	} } int main() {  	int i,j;  	int sum; 	while(scanf("%d%d%d",&n,&m,&t)&&n) 	{  		sum=0; 		f=0; 		memset(flag,0,sizeof(flag)); 		for(i=0;i<n;i++) 		{  			scanf("%s",map[i]); 			for(j=0;j<m;j++)  			{  				if(map[i][j]=='.')  					sum++;  				else if(map[i][j]=='X')  					flag[i][j]=1; //直接处理标记数组,省去地图数组的使用,节约时间  				else if(map[i][j]=='S') 					sx=i,sy=j,flag[i][j]=0;  				//寻找起始点和终点 				else ex=i,ey=j,flag[i][j]=0; 			}  		}  		if((abs(ex-sx)+abs(ey-sy)-t)&1);  		//距离之差减时间为基数,永远不能按时走到终点的迷宫{剪枝} 		//printf("if((abs\n"); 		else if(sum+1>=t) 			flag[sx][sy]=t,dfs(sx,sy,t); 		if(f) 			printf("YES\n"); 		else 			printf("NO\n"); 	}  	return 0; }   


你可能感兴趣的:(HDU1010 Tempter of the Bone)