杭电acm1010 Tempter of the Bone
来源:互联网 发布:js 变量对象 编辑:程序博客网 时间:2024/05/22 02:03
Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 133177 Accepted Submission(s): 35811
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.
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.
'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
典型的深度搜索问题,使用dfs算法求解
package acm1010;/** * date:2017.11.23 * author:孟小德 * function: 杭电acm1010 * Tempter of the Bone 深度搜索 bfs */import java.util.*;class Node{ int row,column; public Node(int row,int column) { this.row = row; this.column = column; } public Node() { }}public class Main{ public static int n,m,T; public static boolean visit[][]; public static int dir[][] = {{1,0},{-1,0},{0,1},{0,-1}}; public static boolean yes; public static Node start = new Node(); public static Node end = new Node(); public static void main(String[] args) { Scanner input = new Scanner(System.in); while (input.hasNextLine()) { n = input.nextInt(); m = input.nextInt(); T = input.nextInt(); input.nextLine(); if (n == 0 && m == 0 && T == 0) { break; } visit = new boolean[n][m]; yes = false; int wall = 0; // 输入 for (int i=0;i<n;i++) { String str = input.nextLine(); for (int j=0;j<m;j++) { char c = str.charAt(j); switch(c) { case 'X':visit[i][j] = true; wall++;break; case 'S':start = new Node(i,j);break; case 'D':end = new Node(i,j);break; case '.':break; } } } if (T>n*m - wall - 1) { System.out.println("NO"); continue; } visit[start.row][start.column] = true; dfs(start.row,start.column,0); if (yes) { System.out.println("YES"); } else { System.out.println("NO"); } } } public static void dfs(int row,int column,int time) { for (int i=0;i<dir.length;i++) { int a = row + dir[i][0]; int b = column + dir[i][1];// 往各个方向搜索 int thetime = time + 1; if (thetime >T) { continue; } if (a<0 || a>=n || b<0 || b>=m) { // 越界 continue; } if (visit[a][b] == true) { // 被访问过 continue; } if (a == end.row && b == end.column) { // 当访问到终点时 if (thetime == T) { yes = true; return; } else { continue; } } // 每次递归都进行剪枝运算 int temp = T - thetime - Math.abs(a - end.row) - Math.abs(b - end.column); if (temp % 2 == 1) { // 当剪枝结果为奇数时,在该点无法在指定时间到达终点 // 此时没必要在这点进行递归,当返回上层递归往另一个方向 // 继续 continue; } visit[a][b] = true; dfs(a,b,thetime); // 同一个点在不同路径中可能被再次访问,因此 // 每一个路径中对应不同的visit visit[a][b] = false; } }}
阅读全文
0 0
- 杭电acm1010 Tempter of the Bone
- 杭电1010-Tempter of the Bone
- 杭电 Tempter of the Bone 1010
- 杭电 1010 Tempter of the Bone
- 杭电1010---Tempter of the Bone
- 杭电 1010 Tempter of the Bone
- 【杭电】[1010]Tempter of the Bone
- 杭电1010 Tempter of the Bone
- 杭电OJ——Tempter of the Bone
- 杭电 HDU 1010 Tempter of the Bone
- 杭电1010Tempter of the Bone(搜索)
- 杭电OJ:1010 Tempter of the Bone
- 杭电1010 Tempter of the Bone(奇偶性剪枝)
- 杭电 HDU ACM 1046 Tempter of the Bone
- 杭电(hdu)ACM 1010 Tempter of the Bone
- 杭电1010-Tempter of the Bone(BFS)
- C - Tempter of the Bone 杭电1010
- 杭电-1010 Tempter of the Bone(深搜)
- cxf
- mysql group_concat( 读取出来的数据长度限制
- centos7安装mysql5.6
- kafka 1.0 中文文档(二) -- APIS
- python 递归算阶乘
- 杭电acm1010 Tempter of the Bone
- 合并两个排序的链表
- vue.js中$nextTick与nextTick的区别
- volatile关键字解析
- 分治算法
- Mac下 Android studio创建签名证书,打包生成APK,.jks文件添加到build.gradle中
- 根据面试题总结的一些初中级java面试知识点
- 【自然语言处理入门】02:Kenlm语料库的制作与模型的训练
- express中间件,一篇文章就够了