Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.74 KB | None | 0 0
  1. package com.yangli907.LNKN;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.LinkedList;
  6. import java.util.List;
  7. import java.util.Queue;
  8.  
  9. /**
  10.  * Given a chessboard represented by 2D array, there's a knight at (0, 0) position.
  11.  * Count how many different ways can the knight moved to (n-1, n-1) position in EXACTLY k steps.
  12.  * There's NO restriction on how many times knight can move to same cell in the board.
  13.  *
  14.  * i.e. given a 4x4 board with k = 2, then the answer is 2.
  15.  *
  16.  * Optionally print out the satisfied path.
  17.  */
  18. public class KnightMoveKSteps {
  19.  
  20.     public static void main(String[] args) {
  21.         int[][] M = new int[4][4];
  22.         int[] s = {0, 0};
  23.         int[] e = {3, 3};
  24.         int k = 3;
  25.         int ways = new KnightMoveKSteps().countWays(M, s, e, k);
  26.  
  27.         System.out.println(ways);
  28.     }
  29.  
  30.     /**
  31.      * BFS, with a queue to store the next possible moves.
  32.      * Use DP[k][i][j] to store the answer for ways to reach cell (i, j) at step k.
  33.      */
  34.     private int countWays(int[][] M, int[] s, int[] e, int k) {
  35.  
  36.         int row = M.length;
  37.         int col = M[0].length;
  38.  
  39.         int[][][] dp = new int[k + 1][row][col];
  40.         dp[0][s[0]][s[1]] = 1;
  41.  
  42.         Queue<int[]> q = new LinkedList<>();
  43.         q.add(s);
  44.         Queue<List<String>> paths = new LinkedList<>();
  45.         paths.add(Arrays.asList("(0, 0)"));
  46.  
  47.         for(int i = 0; i < k; i++) {
  48.  
  49.             int[] dx = {1, 1, -1, -1, 2, 2, -2, -2};
  50.             int[] dy = {2, -2, 2, -2, 1, -1, 1, -1};
  51.             int size = q.size();
  52.  
  53.             for(int j = 0; j < size; j++) {
  54.                 int[] cur = q.remove();
  55.                 List<String> path = paths.remove();
  56.  
  57.                 for(int d = 0; d < 8; d++) {
  58.                     int xx = cur[0] + dx[d];
  59.                     int yy = cur[1] + dy[d];
  60.  
  61.                     if (isValid(M, xx, yy)) {
  62.                         dp[i+1][xx][yy] += dp[i][cur[0]][cur[1]];
  63.                         int[] next = {xx, yy};
  64.                         q.add(next);
  65.                         List<String> copy = new ArrayList<>(path);
  66.                         copy.add("(" + xx + ", " + yy + ")");
  67.                         paths.add(copy);
  68.  
  69.                         if (next[0] == e[0] && next[1] == e[1] && i == k - 1) {
  70.                             System.out.println(copy.toString());
  71.                         }
  72.                     }
  73.                 }
  74.             }
  75.  
  76.         }
  77.  
  78.         for(int i = 0; i < M.length; i++) {
  79.             for(int j = 0; j < dp[0].length; j++) {
  80.                 System.out.print(dp[k][i][j] + "\t");
  81.             }
  82.             System.out.println();
  83.         }
  84.  
  85.         return dp[k][row - 1][col - 1];
  86.     }
  87.  
  88.     private boolean isValid(int[][] M, int x, int y) {
  89.         return x >= 0 && y >= 0 && x < M.length && y < M[0].length;
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement