[JAVA][HDU 1010][Tempter of the Bone]

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. import java.io.BufferedInputStream;  
  2. import java.util.Scanner;  
  3.   
  4. import com.sun.org.apache.regexp.internal.recompile;  
  5.   
  6. public class Main {  
  7.   
  8.     static int row;  
  9.     static int col;  
  10.     static int step;  
  11.     static char maze[][] = new char[20][20];// save the maze  
  12.     static boolean isPosWalked[][] = new boolean[20][20];// check if the pos have been walked  
  13.     static boolean isSuccess;  
  14.   
  15.     public static boolean dfs(char maze[][], int y, int x, int currentStep) {  
  16.         if (maze[y][x] == 'D' && currentStep == step) {  
  17.             isSuccess = true;  
  18.         } else if (maze[y][x] == 'D' && currentStep != step) {  
  19.             return false;// if we got destination but not the requested step,go back to the upper  
  20.         }  
  21.         isPosWalked[y][x] = true;  
  22.         // go right  
  23.         if (x < col - 1 && maze[y][x + 1] != 'X' && !isPosWalked[y][x + 1]) {// if we can walk to the right side  
  24.             dfs(maze, y, x + 1, currentStep + 1);  
  25.         }  
  26.         // go down  
  27.         if (y < row - 1 && maze[y + 1][x] != 'X' && !isPosWalked[y + 1][x]) {// if we can walk to the right side  
  28.             dfs(maze, y + 1, x, currentStep + 1);  
  29.         }  
  30.         // go left  
  31.         if (x > 0 && maze[y][x - 1] != 'X' && !isPosWalked[y][x - 1]) {// if we can walk to the right side  
  32.             dfs(maze, y, x - 1, currentStep + 1);  
  33.         }  
  34.         // go up  
  35.         if (y > 0 && maze[y - 1][x] != 'X' && !isPosWalked[y - 1][x]) {// if we can walk to the right side  
  36.             dfs(maze, y - 1, x, currentStep + 1);  
  37.         }  
  38.         isPosWalked[y][x] = false;  
  39.         return false;  
  40.     }  
  41.   
  42.     public static void print(char maze[][], int y, int x) {  
  43.         for (int i = 0; i < y; i++) {  
  44.             for (int j = 0; j < x; j++) {  
  45.                 System.out.print(maze[i][j]);  
  46.             }  
  47.             System.out.println();  
  48.         }  
  49.     }  
  50.   
  51.     public static void main(String[] args) {  
  52.         Scanner sc = new Scanner(new BufferedInputStream(System.in));  
  53.         row = sc.nextInt();  
  54.         col = sc.nextInt();  
  55.         step = sc.nextInt();  
  56.         int startY = 0, startX = 0;  
  57.         while (row != 0 || col != 0 || step != 0) {  
  58.             for (int i = 0; i < row; i++) {  
  59.                 String tmp = sc.next();  
  60.                 for (int j = 0; j < col; j++) {  
  61.                     maze[i][j] = tmp.charAt(j);// trans str to char  
  62.                     if (maze[i][j] == 'S') {  
  63.                         startY = i;  
  64.                         startX = j;  
  65.                     }  
  66.                 }  
  67.             }  
  68.             isSuccess = false;  
  69.             for (int i = 0; i < 20; i++) {  
  70.                 for (int j = 0; j < 20; j++) {  
  71.                     isPosWalked[i][j] = false;  
  72.                 }  
  73.             }  
  74.             dfs(maze, startY, startX, 0);  
  75.             if (isSuccess) {  
  76.                 System.out.println("YES");  
  77.             } else {  
  78.                 System.out.println("NO");  
  79.             }  
  80.             // print(maze, y, x);  
  81.   
  82.             row = sc.nextInt();  
  83.             col = sc.nextInt();  
  84.             step = sc.nextInt();  
  85.         }  
  86.     }  
  87. }  


最近写的题目多用到dfs,一年前用C没写出来一直WA,今天看到hdu的Discussion有同学提供了很多测试数据,逐个把答案不同的调试一下终于AC

有这么几个要点:

1.

2 2 3
SD
..

这样的testcase也是要输出YES的,转了一个圈什么的,所以首次到达Destination也要判断一下是否和Required-Step完全符合


2.其实也是比较基础的,返回到upper recrusion的时候要注意将各种值回归进入时的状态

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值