程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> HDU 1010 Tempter of the Bone ( DFS(深度優先搜索) )

HDU 1010 Tempter of the Bone ( DFS(深度優先搜索) )

編輯:C++入門知識

Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 54622 Accepted Submission(s): 14723

 


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

迷宮問題:
思路:DFS(深度優先搜索),+ 奇偶剪枝

import java.io.*;
import java.util.*;
public class Main {
	public static int n,m,t,sx,sy,dx,dy;
	public static char ch[][];
	public static int fx[]={0,0,1,-1};
	public static int fy[]={1,-1,0,0};
	public static boolean boo;
	public static void main(String[] args) {
		Scanner sc=new Scanner(new BufferedInputStream(System.in));//輸入
		PrintWriter pw=new PrintWriter(new BufferedOutputStream(System.out),true);//輸出
		while(sc.hasNextInt()){
			n=sc.nextInt();
			m=sc.nextInt();
			t=sc.nextInt();
			if(n==0&&m==0&t==0)
				break;
			boo=false;
			ch=new char[n][m];
			for(int i=0;i<n;i++){
				String s=sc.next();
				ch[i]=s.toCharArray();
			}
			int week=0;
			for(int i=0;i<ch.length;i++){
				for(int j=0;j<ch[0].length;j++){
					if(ch[i][j]=='S'){
						sx=i;
						sy=j;
					}
					if(ch[i][j]=='D'){
						dx=i;
						dy=j;
					}
				}
			}
			
			ch[sx][sy]='X';
			DFS(sx,sy,0);
			if(boo)
				pw.println("YES");
			else
				pw.println("NO");
		}
		
	}

	public static void DFS(int sx,int sy,int t1){
		if(boo)
			return;
		if(sx==dx&&sy==dy&&t1==t){
			boo=true;
			return;
		}
		//奇偶剪枝
		int min = Math.abs(sx - dx) + Math.abs(sy - dy); // 最短時間
		if((t1%2!=0)&&(t-min)%2==0)
			return;
		if (t1 + min > t)//如果走過的時間加上最短時間大於規定時間,就返回  
            return;  
		
		// 四個方向進行判斷
		for(int i=0;i<4;i++){
			int ffx=sx+fx[i];
			int ffy=sy+fy[i];
			if(check(ffx,ffy)){
				ch[ffx][ffy]='X';
				DFS(ffx,ffy,t1+1);
				ch[ffx][ffy]='.';//回溯
			}
		}
	}
	public static boolean check(int sx,int sy){
		if(sx<0||sy<0||sx>n-1||sy>m-1||ch[sx][sy]=='X')
			return false;
		 return true;
	}
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved