A - 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.
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  
这道题虽然是用dfs,但就算想到了这点,也还是刚开始而已,其实大多数用dfs能解的题都如此,剪枝才是关键,否则以dfs巨大的时间消耗铁定会超时;于是就在这
上面LE了好几次,最后看大佬的做法才发现要用到奇偶剪枝,路径键枝;
关于奇偶,是在矩阵中要你从一点到另一点刚好用限定的步伐; 
  
  
  1. 把矩阵标记成如下形式:
  2. 0,1,0,1,0
  3. 1,0,1,0,1
  4. 0,1,0,1,0
  5. 1,0,1,0,1
  6. 很明显,如果起点在0 而终点在1 那显然 要经过奇数步才能从起点走到终点,依次类推,奇偶相同的偶数步,奇偶不同的奇数步
  7. 在读入数据的时候就可以判断,并且做剪枝,当然做的时候并不要求把整个矩阵0,1刷一遍,读入的时候起点记为(Si,Sj) 终点记为(Di,Dj) 判断(Si+Sj) 和 (Di+Dj) 的奇偶性就可以了
  1. 路径剪枝:
  2. 矩阵的大小是N*M 墙的数量记为wall 如果能走的路的数量 N*M - wall 小于时间T,就是说走完也不能到总的时间的,这显然是错误的,可以直接跳出了
 
 
 
 
 
 
#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
char map[9][9];
int n,m,t,x,dx,y,dy,ok;
int a[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
void dfs(int x,int y,int time)
{
	int i,ans;
	if(x==dx&&y==dy&&time==t) ok=1;
	if(ok) return;
	if(x<1||y<1||x>n||y>m) return;  //搜索到边界 。 
	ans=t-time-abs(dx-x)-abs(dy-y); //奇偶剪枝,感觉好吊。
	if(ans<0||ans&1) return;
	for(i=0;i<4;i++)
	{
		if(map[x+a[i][0]][y+a[i][1]]!='X') 
		{
			map[x+a[i][0]][y+a[i][1]]='X';
			dfs(x+a[i][0],y+a[i][1],time+1);
			map[x+a[i][0]][y+a[i][1]]='.';
		}
	}
}
int main()
{
	int i,j,k;
	while(scanf("%d%d%d",&n,&m,&t)!=EOF)
	{
		getchar();  //注意这个getchar();不能省略,否则输出一直是NO,找了1个多小时,无奈了。 
		if(n==0&&m==0&&t==0)
		    break;
		for(i=1,k=0;i<=n;i++)
		{
			for(j=1;j<=m;j++)
			{
				scanf("%c",&map[i][j]);
				if(map[i][j]=='S') 
				{
				    x=i;
					y=j; 
				}
			    if(map[i][j]=='D')
				{
					dx=i;
					dy=j;
				}
				if(map[i][j]=='X')
				    k++;	
			}
			getchar();
		}
		if(n*m-k<t)  //最长的搜索时间。 
		   printf("NO\n");
		else{
		ok=0;
		map[x][y]='X';  //标记已经访问过了。 
		dfs(x,y,0);
		if(ok==1)
		    printf("YES\n");
		else    
		    printf("NO\n");}
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值