HDU-1010-java实现 奇偶剪枝

Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 138326    Accepted Submission(s): 37026


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 5S.X...X...XD....3 4 5S.X...X....D0 0 0
 

Sample Output
 
 
NOYES



搜索题,原本看到迷宫感觉应该用宽度优先搜索,但是看了DISCUSS发现都是dfs。仔细思考后发现确实是这样,因为这道题目是要求在特定的第T秒到达门前,而不是最少需要几秒。我们要搜索出一个路径使得恰好在t秒到门前,而我们可以刻意地“绕路”,来达到这么一个要求。所以说还是要要用深搜,在四个方向上进行搜索,也就是说我随便哪个方向走,反正在第T秒我要到门前就行了。

这道题还有要注意的就是这样会超时,我们要剪枝,剪枝的道理其实很简单,不过直接想恐怕也想不到,我也是参考了别人的。
其实就是奇偶剪枝。就是说,我们把这个路径看成一个矩阵:

0 1 0 1 0 1
1 0 1 0 1 0

0 1 0 1 0 1
1 0 1 0 1 0

那么从相同的数字到相同的数字需要的步数一定是偶数。走到不同的数字一定是奇数。不管我怎么绕路,一定是这样。因为绕路需要的步数一定是偶数。那么如果我发现我需要的步数一定是偶数,而你时间却只给奇数秒,那么我就一定到不了。原理就是这个。其实很简单,很容易理解。
贴上代码:

import java.util.Scanner;
public class Main {
            static int N;
            static int M;
            static int T;
            static int dir[][]={{0,1},{-1,0},{0,-1},{1,0}};
            static char[][] foot;
            static Node[] aim;
            static boolean[][] vis;
            static int door;
	  public static void main(String[] args) {
		// TODO 自动生成的方法存根
           Scanner cin=new Scanner(System.in);
           while(cin.hasNext())
           {   
        	   door=0;
        	   N=cin.nextInt();
        	   M=cin.nextInt();
        	   T=cin.nextInt();
        	   aim=new Node[T+1];    	   
        	   vis=new boolean[N+1][M+1];
        	   if(N==0&&M==0&&T==0)break;
        	   foot=new char[N+1][M+1];
        	   for(int i=1;i<=N;i++)
        	   {
        		   String str=cin.next();
        		   for(int j=0;j<M;j++)
        		   {
        			   foot[i][j+1]=str.charAt(j);
        			   if(foot[i][j+1]=='D')door=i+j+1;
        		   }
        	   }
        	   for(int i=1;i<=N;i++)
        	   {
        		   for(int j=1;j<=M;j++)
        		   {
        			   if(foot[i][j]=='S'){
        				   aim[0]=new Node(i,j);
        			       vis[i][j]=true;}
        		   }
        	   }
        	   if(dfs(0))System.out.println("YES");
        	   else System.out.println("NO");
           }
	}
	static boolean dfs(int start)
	{
		
		if(start==T)
		{
			if(foot[aim[T].x][aim[T].y]=='D'){
				return true;
			}
		}else
		{
			Node pre=aim[start];
			int rem=T-start;
			int need=Math.abs(pre.x+pre.y-door);
			if(rem<need||(rem-need)%2!=0)return false;   //剪枝
			for(int i=0;i<4;i++)
			{
				int x=pre.x+dir[i][0];
				int y=pre.y+dir[i][1];
				if(x>0&&y>0&&x<=N&&y<=M&&foot[x][y]!='X'&&!vis[x][y]){
					vis[x][y]=true;
					aim[start+1]=new Node(x,y);
					if(dfs(start+1))return true;
					//dfs(start+1);
					vis[x][y]=false;
				}
			}
		}
		return false;
	}

}
class Node
{
	int x;
	int y;
	public Node(int xx,int yy)
	{
		x=xx;
		y=yy;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值