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

    即给定一个N*N的棋盘,棋盘上有一个骑士,初始位置为r行c列,骑士将会移动K步。

    其中(0,0)为左上角,(N-1,N-1)为右下角

    骑士可以按如图所示方法移动

    骑士将停止移动直到走出棋盘或者走完K步

    求骑士走K步后还留在棋盘的概率

    问题解决:

    走K步还留在棋盘的概率,即走K步后留在棋盘的情况数量除以总的情况数量

    总的情况数量为8的K次方

    定义一个N*N二维数组dp[i][j],初始化为1,表示骑士从i,j位置走0步后还留在棋盘的情况数量,之后定义循环,k从1遍历到K,每次遍历用newDp更新dp,得到走完新的一步后还留在棋盘的情况数量

    定义一个N*N二维数组newDp[i][j],初始化为0,用于存从i,j位置走第k步后还留在棋盘的情况数量,所以是从i,j往8个方向走一步到新的位置,新的位置再走k-1步得到k步,所以newDp[i][j]最后的值是遍历8个方向判断新的位置是否在棋盘内,若新的位置在棋盘内,则newDp[i][j]加上新的位置走k-1步后留在棋盘的情况数量即dp[new_i][new_j]

    遍历完整个N*N棋盘后得到走k步后还留在棋盘的情况数量后,将dp更新为newDp

    遍历完K步后,dp[r][c]即为从r行c列开始走K步后仍留在棋盘的情况数量

    dp[r][c]/pow(8,K)即为最后的结果

    代码如下:

    class Solution {
    public:
        double knightProbability(int N, int K, int r, int c) {
            //若走0步,肯定留在棋盘内,返回1
            if(K == 0) return 1;
            //若不是走0步且棋盘小于等于2*2,不可能留在棋盘内,返回0
            if(N <= 2) return 0;
            //定义8个方向
            vector<vector<int>> move{{-2,-1},{-1,-2},{1,-2},{2,-1},{-2,1},{-1,2},{1,2},{2,1}};
            //初始化dp,即从i,j走0步后留在棋盘的情况数量都为1
            vector<vector<double>> dp(N,vector<double>(N,1));
            
            int i, j, k, m;
            //遍历K次,每次得到在棋盘各个位置走k步后留在棋盘的情况数量
            for(k = 0; k < K; k++) {
                //初始化newDp
                vector<vector<double>> newDp(N,vector<double>(N,0));
                //遍历棋盘每个位置
                for(i = 0; i < N; i++) {
                    for(j = 0; j < N; j++) {
                        //遍历8个方向
                        for(m = 0; m < 8; m++) {
                            int newR = i+move[m][0], newC = j+move[m][1];
                            //若新的位置在棋盘内,则加上新的位置走k-1步后留在棋盘的情况数量,即上一次循环得到的dp
                            if(newR >= 0 && newR <= N-1 && newC >= 0 && newC <= N-1) {
                                newDp[i][j] += dp[newR][newC];
                            }
                        }
                    }
                }
                //更新dp
                dp = newDp;
            }
            //算比例
            double res = dp[r][c]/pow(8,K);
            return res;
        }
    };



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值