HDU1010Tempter of the Bone (深搜+剪枝模板题)

6 篇文章 0 订阅

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

题意就是让小狗从S点到D点,且花费的时间恰好是T,注意是恰好,不是在T时间内

所以不能用广搜,起初我也想用广搜来做,思维固定了,一看这种到达终点的问题就想BFS,太傻了

原因:小狗只能在一个地方停留1秒,如果BFS成功,到达终点D,如果花费的时间不等于T,那小狗就狗带了(活埋)

这个题应该算是剪枝的模板题了

1.先讲一下曼哈顿距离,假如有两个点x1,y1,x2,y2,他们的曼哈顿距离为Math.abs(x1-x2)+Math.abs(y1-y2);

  即每次只能向上向下向左向右移动的最近距离,我们的时间T-曼哈顿距离必须满足为偶数

  原因:假如为奇数,不可能存在这种路径,因为在这种移动方式下,差为奇数不成环,具体画画图就一清二楚了

   

2.剪枝

  首先就将T-曼哈顿距离(S,D)差为奇数的减去,立flag,狗带

  其次,一旦满足条件,flag=1,立即返回,                       狗活了

  时间>=T,返回门关了,                                                    狗带

  。。。超界,下一步已被标记,撞墙, 剪枝,                  狗带

  时间<T,没被标记,可以走,深搜下一步,注意还原标记,时间+1,一旦满足条件,立即返回(剪枝)

 

我也是参考的大神的,感觉讲的很好,附上链接,上面看不懂,看大神的

大神链接:https://blog.csdn.net/ltyqljhwcm/article/details/50905284

import java.util.Arrays;
import java.util.Scanner;

public class Main {
	static int N,M,T,bx,by,ex,ey,flag;
	static char[][] map=new char[9][9];
	static boolean[][] book=new boolean[9][9];
	static int[][] dis={{0,1},{0,-1},{1,0},{-1,0}};
	static void dfs(int x,int y,int index){
		for(int i=0;i<4;i++){
			int dx=x+dis[i][0];
			int dy=y+dis[i][1];
			if(dx==ex&&dy==ey&&index+1==T){
				flag=1;
				return;
			}
			
			if(index>=T){
				return;
			}else if(dx<0||dx>=N||dy<0||dy>=M||map[dx][dy]=='X'||book[dx][dy]==true){
				continue;
			}else{
				if(book[dx][dy]==false&&map[dx][dy]=='.'){
					book[dx][dy]=true;
					dfs(dx,dy,index+1);
					
					if(flag==1)
						return;
					book[dx][dy]=false;
				}
			}
		}
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc=new Scanner(System.in);
		N=sc.nextInt();
		M=sc.nextInt();
		T=sc.nextInt();
		while(N!=0&&M!=0&&T!=0){
			flag=0;			
			for(int i=0;i<9;i++)
				Arrays.fill(book[i],false);
			for(int i=0;i<N;i++){
				String s=sc.next();
				for(int j=0;j<s.length();j++){
					map[i][j]=s.charAt(j);	
					if(map[i][j]=='S'){bx=i;by=j;}
					if(map[i][j]=='D'){ex=i;ey=j;}						
				}								
			}			
			int sum=Math.abs(bx-ex)+Math.abs(by-ey);
			if((sum-T)%2==1);
			else{
				book[bx][by]=true;
				dfs(bx,by,0);
			}			
			if(flag==1)
				System.out.println("YES");
			else
				System.out.println("NO");				
			N=sc.nextInt();
			M=sc.nextInt();
			T=sc.nextInt();
		}
		
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值