Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. static int[] di = { 0, 0, 1, -1 };
  2. static int[] dj = { 1, -1, 0, 0 };
  3.  
  4. public int maxJumps(String[] board) {
  5. char[][] a = new char[board.length][board[0].length()];
  6. for (int i = 0; i < board.length; i++)
  7. a[i] = board[i].toCharArray();
  8.  
  9. if (a[0][0] == 'H')
  10. return 0;
  11.  
  12. int[][] dp = new int[a.length][a[0].length];
  13. boolean[][] v = new boolean[a.length][a[0].length];
  14.  
  15. for (int[] i : dp)
  16. Arrays.fill(i, -1);
  17. dp[0][0] = 1;
  18. v[0][0] = true;
  19.  
  20. int res = dfs(0, 0, dp, v, a, 1);
  21.  
  22. return res;
  23.  
  24. }
  25.  
  26. private int dfs(int i, int j, int[][] dp, boolean[][] v, char[][] a, int c) {
  27. int m = a[i][j] - '0';
  28.  
  29. int max = 1;
  30.  
  31. for(int d = 0; d < 4; d++){
  32. int ii = i + m*di[d];
  33. int jj = j + m*dj[d];
  34.  
  35. if(ii < 0 || ii >= a.length || jj < 0 || jj >= a[0].length){max = Math.max(max, c); continue;}
  36. if(a[ii][jj] == 'H'){max = Math.max(max, c); continue;}
  37.  
  38. if(v[ii][jj])return -1;
  39.  
  40. if(c+1 > dp[ii][jj]){
  41. v[ii][jj] = true;
  42.  
  43. dp[ii][jj] = c+1;
  44.  
  45. int ret = dfs(ii, jj, dp, v, a, c+1);
  46. if(ret == -1)return -1;
  47.  
  48. max = Math.max(max, ret);
  49.  
  50. v[ii][jj] = false;
  51. }
  52.  
  53. }
  54.  
  55. return max;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement