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

描述:

小狗在一个古老的迷宫里找到了一根骨头,这使他非常着迷。然而,当他捡起来时,迷宫开始摇晃,小狗可以感觉到地面在下沉。他意识到骨头是个陷阱,他拼命想离开这个迷宫。
迷宫是一个长方形,大小为n/m。迷宫里有一扇门。在开始的时候,门是关着的,它会在t-秒打开一段很短的时间(不到1秒)。因此,小狗必须在t-秒正好到达门口。在每一秒,他可以移动一个街区到一个上下左右相邻的街区。一旦他进入一个街区,这个街区的地面就会开始下沉,并在下一秒消失。他不能在一个街区停留超过一秒,也不能进入一个访问过的街区。可怜的小狗能活下来吗?请帮帮他

输入:

输入由多个测试用例组成。每个测试用例的第一行包含三个整数n、m和t(1<n,m<7;0<t<50),分别表示迷宫的大小和开门的时间。接下来的n行给出了迷宫布局,每行包含m字符。一个字符是以下其中之一:

'X':一堵墙,狗不能进入;

'S':狗的起点;

'D':门;或

'.':一个空街区。

输入以三个0结尾。此测试用例不被处理。

输出:

对于每个测试用例,在一行中打印“YES”,如果小狗可以存活,或“NO”。

AC码:

#include<stdio.h>
#include<math.h>
int sum,t,m,n,e,ex,ey;
char map[7][8];
struct direction
{
	int x;
	int y;
}d[4]={{0,1},{1,0},{0,-1},{-1,0}};
void fun(int sx,int sy)
{
	if(map[sx][sy]=='D')//首先判断起始坐标是不是终点 
	{
		if(sum==t)//若是终点,判断跳跃次数是否与要求相同 
		{ 
			printf("YES\n");//输出YES,e变化,并结束当前函数的调用 
			e++;
			return;
		}
		else 
		{
			sum--;//因为之前到终点跳的这一次加上了,但却又不符合条件,故再减掉 
			return;
		}
	}
	if((t-sum-(abs(ex-sx)+abs(ey-sy)))%2!=0)
	{//奇偶剪枝:在进行深搜前就可排除一些不可能的坐标,防超时 
		sum--;
		return;
	}
	int tx,ty,i;//用tx,ty 表示下一个坐标并带入递归,可保存上一个坐标 
	map[sx][sy]='X';//使得深搜时不会再搜之前的点 
	for(i=0;i<4;i++)//一共有4个方向,每个方向都要进行搜查 
	{
		tx=sx+d[i].x;//结构体数组d[4]记录了每个方向的偏移量,可使点发生移动 
		ty=sy+d[i].y;//例如,现位于(3,4)处,向上移动一个 , x 不变 y+1 则使用d[0] 
		if(tx>=0&&tx<n&&ty>=0&&ty<m&&map[tx][ty]!='X')	 
		{//不越界且不是墙
			sum++;//可以过去,跳一次 
			fun(tx,ty);//进行递归,暴力搜查。将现在的坐标作为起始点带入函数 
		}			  //这样后,每个点都会搜查4个方向,若下一个点搜查完毕,就会返回上一个点
	}													
	map[sx][sy]='.';//若4个方向访问结束,便将'X'变回'.'表明退了回去,点重置,上一点换方向改变路线时,此点仍可过 
	sum--;//退了回去,次数减1 
}
int main()
{ 
	int i,u,sx,sy;
	while(~scanf("%d%d%d",&n,&m,&t)&&(n!=0&&m!=0&&t!=0))
	{ 
		sum=0;e=0;//将题中时刻相同理解成 sum :表示跳了几次,用sum与t进行比较 
		getchar();//由于回车也是字符,如不消除会存入数组,使用getchar()吸收回车 
		for(i=0;i<n;i++)
		{
			for(u=0;u<m;u++)
			{
				scanf("%c",&map[i][u]);//双重循环,对全局二维数组map 进行赋值 
				if(map[i][u]=='S')//记录开始坐标 
				{
					sx=i;
					sy=u;
				}
				if(map[i][u]=='D')//记录终点坐标 
				{
					ex=i;
					ey=u;
				} 
			}		 
			getchar();//每行输入结束时会再输入一个回车键,再次吸收掉 
		} //到此基本图表输入与记录完成 
		fun(sx,sy);//将起始坐标带入函数 
		if(e==0) printf("NO\n");//此处用e来判断:e为全局变量,若在函数中输出YES,e值会变化,则不会输出NO 
	}
return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值