Tempter of the Bone(深度搜索)

描述:

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.


輸入:

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.


輸出:

For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.



樣咧輸入:

4 4 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0


樣咧輸出:

NO

YES



題目大意:

輸入地圖信息和時間t;一隻狗所在的起始位置爲S大門位置爲D,‘X’表示不能通過。狗每次向左或向右或向上或向下一步需要耗時1s問能否在ts時剛好到達D.



/*
此題爲簡單的深搜但要注意奇偶剪枝否則容易超時 */
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int x,y,t,x2,y2,flag,time;
char map[10][10];
void dfs(int a,int b)
{
	if(a==x2&&b==y2&&time==t)
	{
		flag=1;
		return;
	}
	if(a<0||b<0||a>=x||b>=y)
	return;
	if(map[a+1][b]=='.')								//分四個方向搜索 
	{
		map[a+1][b]='X';
		time++;
		dfs(a+1,b);
		map[a+1][b]='.';								//回溯時別忘回退 
		time--;
	}
	if(map[a-1][b]=='.')
	{
		map[a-1][b]='X';
		time++;
		dfs(a-1,b);	
		map[a-1][b]='.';
		time--;			
	}
	if(map[a][b+1]=='.')
	{
		map[a][b+1]='X';
		time++;
		dfs(a,b+1);	
		map[a][b+1]='.';
		time--;	
	}
	if(map[a][b-1]=='.')
	{
		map[a][b-1]='X';
		time++;
		dfs(a,b-1);	
		map[a][b-1]='.';
		time--;	
	}			
}
int main()
{
	int x1,y1;
	while(scanf("%d %d %d",&x,&y,&t),x!=0&&y!=0&&t!=0)
	{
		flag=0;
		time=0;
		for(int i=0;i<x;i++)
		{
			scanf("%s",map[i]);
		}
		for(int i=0;i<x;i++)
		{
			for(int j=0;j<y;j++)
			{
				if(map[i][j]=='D')
				{
					x2=i;
					y2=j;
				}
				if(map[i][j]=='S')
				{
					x1=i;
					y1=j;
				}				
			}
		}
		map[x1][y1]='X';
		map[x2][y2]='.';
		if((abs(x1-x2)+abs(y1-y2)-t>0)||((abs(x1-x2)+abs(y1-y2))%2==0&&t%2!=0)||((abs(x1-x2)+abs(y1-y2))%2!=0&&t%2==0))      //奇偶剪枝 
		printf("NO\n");
		else
		{
			dfs(x1,y1);												//深搜 
			if(flag==1)
			printf("YES\n");
			else
			printf("NO\n");
		}
	}
	return 0;
}


奇偶剪枝鏈接:

http://baike.sogou.com/v72712602.htm?fromTitle=%E5%A5%87%E5%81%B6%E5%89%AA%E6%9E%9D

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章