Monday, October 15, 2018

Leetcode 688. Knight Probability in Chessboard

On an NxN chessboard, a knight starts at the r-th row and c-th column and attempts to make exactly K moves. The rows and columns are 0 indexed, so the top-left square is (0, 0), and the bottom-right square is (N-1, N-1).
A chess knight has 8 possible moves it can make, as illustrated below. Each move is two squares in a cardinal direction, then one square in an orthogonal direction.


Each time the knight is to move, it chooses one of eight possible moves uniformly at random (even if the piece would go off the chessboard) and moves there.
The knight continues moving until it has made exactly K moves or has moved off the chessboard. Return the probability that the knight remains on the board after it has stopped moving.

Example:
Input: 3, 2, 0, 0
Output: 0.0625
Explanation: There are two moves (to (1,2), (2,1)) that will keep the knight on the board.
From each of those positions, there are also two moves that will keep the knight on the board.
The total probability the knight stays on the board is 0.0625.

Note:
  • N will be between 1 and 25.
  • K will be between 0 and 100.
  • The knight always initially starts on the board.

Analysis:
This is a DP problem. We use dp[K + 1][N][N], where dp[k][i][j] represnts after k steps, how many ways we can reach to the (i, j). So the initial state would be dp[0][i][j] = 1. Final state would be dp[K][r][c].

Code (Java):
class Solution {
    int[][] dirs = {{-2, -1}, {-1, -2}, {-2, 1}, {-1, 2}, {2, -1}, {1, -2}, {1, 2}, {2, 1}};
    public double knightProbability(int N, int K, int r, int c) {
        double[][][] dp = new double[K + 1][N][N];
        
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                dp[0][i][j] = 1;
            }
        }
        
        for (int step = 1; step <= K; step++) {
            for (int i = 0; i < N; i++) {
                for (int j = 0; j < N; j++) {
                    for (int[] dir : dirs) {
                        int x = i + dir[0];
                        int y = j + dir[1];
                        if (x < 0 || x >= N || y < 0 || y >= N) {
                            continue;
                        }

                        dp[step][i][j] += dp[step - 1][x][y];
                    }
                }
            }
        }

        return dp[K][r][c] / Math.pow(8, K);
    }
}

Another DP solution:
Another way of thinking this problem is, dp[k][i][j] means in the step k, the number of ways from (r, c) to (i, j). So the initial state would be dp[0][r][c] = 1;

The final state is to sum up all the ways on the board.

Code (Java):
class Solution {
    int[][] dirs = {{-2, -1}, {-1, -2}, {-2, 1}, {-1, 2}, {2, -1}, {1, -2}, {1, 2}, {2, 1}};
    public double knightProbability(int N, int K, int r, int c) {
        double[][][] dp = new double[K + 1][N][N];
        
        dp[0][r][c] = 1;
        
        for (int step = 1; step <= K; step++) {
            for (int i = 0; i < N; i++) {
                for (int j = 0; j < N; j++) {
                    for (int[] dir : dirs) {
                        int x = i + dir[0];
                        int y = j + dir[1];
                        if (x < 0 || x >= N || y < 0 || y >= N) {
                            continue;
                        }

                        dp[step][i][j] += dp[step - 1][x][y];
                    }
                }
            }
        }

        double sum = 0;
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                sum += dp[K][i][j];
            }
        }
        
        return sum / Math.pow(8, K);
    }
}

No comments:

Post a Comment