zoukankan      html  css  js  c++  java
  • LeetCode 688. Knight Probability in Chessboard “马”在棋盘上的概率 (C++/Java)

    题目:

    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.

    分析:

    给定一个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);
        }
    }
  • 相关阅读:
    Linux内核之 I/O多路复用
    Linux内核之 页高速缓存与页回写
    Linux内核之 块I/O层及I/O调度
    Linux内核之 文件I/O
    C++雾中风景15:聊聊让人抓狂的Name Mangling
    音频数据增强及python实现
    深度学习中“过拟合”的产生原因和解决方法
    资料-资源(不定期更新)
    论文翻译:2020_Acoustic Echo Cancellation Challenge Datasets And Testingframework
    语音信号处理概念
  • 原文地址:https://www.cnblogs.com/silentteller/p/12353901.html
Copyright © 2011-2022 走看看