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 steps后,马还落在棋盘上的概率。直观的想法就是,找p(n)到p(n + 1)的变化公式。可是整个棋盘来算概率不好算,于是想到将总的概率分到每一格上,算每一格到下一格的概率变化,根据马的行走特点,可以得到转移方程:f[r][c][steps]=∑​r_change,c_change​​ f[r+r_change][c+c_change][steps−1]/8.0,其中(r_change,c_change) 为(2, 1),(2,1), (2, -1),(2,−1), (-2, 1),(−2,1), (-2, -1),(−2,−1), (1, 2),(1,2), (1, -2),(1,−2), (-1, 2),(−1,2), (-1, -2)(−1,−2)。初始状态f[r][c][0] = 1,用DP算法即可得到最终每一格的概率,求和就是最后的总概率。

具体步骤:

  • step1:遍历所有位置,对概率不为0的位置进行运算。
  • step2:遍历马能跳到的8个位置,遇到在棋盘内的,就将概率除以8后加到新的位置上。
  • step3:进行K次概率迭代,得到K步以后每个位置马落到的概率。
  • step4:对所有位置概率求和,得到马落在棋盘上的总概率。

代码:

#include <iostream>
#include <vector>
using namespace std;

class Solution {
public:
    double knightProbability(int N, int K, int r, int c) {
        vector<vector<double> > v(N, vector<double>(N, 0));
        v[r][c] = 1;
        int r_change[8] = {2,2,1,1,-1,-1,-2,-2};
        int c_change[8] = {1,-1,2,-2,2,-2,1,-1};
        while (K--){
            vector<vector<double> > v1(N, vector<double>(N, 0));
            for (int i = 0; i < N; i++){
                for (int j = 0; j < N; j++){
                    if (v[i][j] != 0){          //落在上面概率为0的点不用计算 
                        for (int k = 0; k < 8; k++){    //遍历每一个可能跳到的位置 
                            int new_r = i + r_change[k];
                            int new_c = j + c_change[k];
                            //如果落在棋盘上,则需要增加此位置的概率 
                            if (new_r >= 0 && new_r < N && new_c >= 0 && new_c < N){
                                v1[new_r][new_c] += v[i][j] / 8.0;
                            }
                        }                       
                    }
                }
            }
            v = v1;     //迭代求下一步的概率 
        }
        //对所有位置的概率求和,得到落在棋盘上的总概率 
        double sum = 0;
        for (int i = 0; i < N; i++){
            for (int j = 0; j < N; j++){
                sum += v[i][j];
            }
        }
        return sum;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值