题目:

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.

LeetCode 688. Knight Probability in Chessboard “马”在棋盘上的概率 (C++/Java)-LMLPHP

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.

分析:

给定一个N*N大小的棋盘,求走K步之后,马还停留在棋盘上的概率,规定马走出棋盘后就不再跳回来。

那么我们知道马可以向八个方向去跳,我们可以求出跳k步之后,在棋盘上停留的位置数,也就是k步后的情况个数,而每次跳8个方向,一共有8^K中情况,最后求比值就是概率。

通过dp二维数组保存前一步马所在的位置,遍历棋盘上每一个位置,再遍历八个方向,如果符合要求,即没有跳出棋盘,就将前一步所在位置的数量加到新的数组中,然后将新数组重新赋值给dp,继续下一步的遍历即可,最后返回位置数的和除以8^K便是答案。

程序:

C++

class Solution {
public:
    double knightProbability(int N, int K, int r, int c) {
        vector<vector<double>> dp(N, vector<double>(N, 0.0));
        dp[r][c] = 1.0;
        int steps[8][2] = {{1, 2}, {2, 1}, {2, -1}, {1, -2}, {-1, -2}, {-2, -1}, {-2, 1}, {-1, 2}};
        for(int k = 0; k < K; ++k){
            vector<vector<double>> temp(N, vector<double>(N, 0.0));
            for(int i = 0; i < N; ++i){
                for(int j = 0; j < N; ++j){
                    for(int l = 0; l < 8; ++l){
                        int x = i + steps[l][0];
                        int y = j + steps[l][1];
                        if(x < 0 || x >= N || y < 0 || y >= N)
                            continue;
                        temp[x][y] += dp[i][j];
                    }
                }
            }
            swap(dp, temp);
        }
        double sum = 0.0;
        for(int i = 0; i < N; ++i)
            for(int j = 0; j < N; ++j)
                sum += dp[i][j];
        return sum / pow(8, K);
    }
};

Java

class Solution {
    public double knightProbability(int N, int K, int r, int c) {
        double[][] dp = new double[N][N];
        dp[r][c] = 1.0;
        int[][] steps = new int[][]{{1, 2}, {2, 1}, {2, -1}, {1, -2}, {-1, -2}, {-2, -1}, {-2, 1}, {-1, 2}};
        for(int k = 0; k < K; ++k){
            double[][] temp = new double[N][N];
            for(int i = 0; i < N; ++i){
                for(int j = 0; j < N; ++j){
                    for(int l = 0; l < 8; ++l){
                        int x = i + steps[l][0];
                        int y = j + steps[l][1];
                        if(x < 0 || x >= N || y < 0 || y >= N)
                            continue;
                        temp[x][y] += dp[i][j];
                    }
                }
            }
            dp = temp;
        }
        double sum = 0.0;
        for(int i = 0; i < N; ++i)
            for(int j = 0; j < N; ++j)
                sum += dp[i][j];
        return sum / Math.pow(8, K);
    }
}
04-14 12:04