设为首页 加入收藏

TOP

HDU 1010 Tempter of the Bone ( DFS(深度优先搜索) )
2014-11-23 19:33:23 来源: 作者: 【 】 浏览:17
Tags:HDU 1010 Tempter the Bone DFS 深度 优先 搜索

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 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;
	}
}

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇HDOJ4561 连续最大积 下一篇HDU 4584 Building bridges

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·C语言中,“指针”用 (2025-12-26 15:20:18)
·在c语言的指针运算中 (2025-12-26 15:20:15)
·C语言-函数指针与函 (2025-12-26 15:20:12)
·求navicat for mysql (2025-12-26 13:21:33)
·有哪位大哥推荐一下m (2025-12-26 13:21:30)