leetcode编程记录12 #688 Knight Probability in Chessboard

leetcode编程记录12 #688 Knight Probability in Chessboard

标签(空格分隔): leetcode


这次的题目是一道有关于动态规划的题目,是一道比较难的题目,要才用一些特殊的方法才能解决,题目如下:

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,棋子马的位置(c, r)和马移动的步数k,然后根据这些信息来计算k步以后马留在棋盘上的概率。首先我们要知道怎样获得这个概率,这个概率实际上是等于马在移动了k步之后能够留在棋盘上的位置数目的叠加再除以马所有可能移动的位置数。后一个数很好算,马每次行棋都有8种可能所以k次迭代后就是8^k种可能,而前一个的可能树目可以通过保存棋盘的上一次每个方格的行棋数来迭代计算出最后的结果。代码如下:

class Solution {
public:

    double knightProbability(int N, int K, int r, int c) {
        if (K == 0) {return 1;}
        if (K == 1) {return findPosition(N, r, c) / 8.0;}
        double state[K + 1][N][N];
        for (int j = 0; j < N; ++j)
            {
                for (int k = 0; k < N; ++k)
                {
                    state[0][j][k] = 1;
                }
            }
        for (int i = 1; i < K + 1; ++i)
        {
            for (int j = 0; j < N; ++j)
            {
                for (int k = 0; k < N; ++k)
                {
                    state[i][j][k] = 0;
                }
            }
        }
        for (int i = 1; i < K + 1; ++i)
        {
            for (int j = 0; j < N; ++j)
            {
                for (int k = 0; k < N; ++k)
                {
                    if (k + 2 < N && j + 1 < N) {state[i][j][k] += state[i - 1][j + 1][k + 2];}
                    if (k + 1 < N && j + 2 < N) {state[i][j][k] += state[i - 1][j + 2][k + 1];}
                    if (k + 2 < N && j - 1 > -1) {state[i][j][k] += state[i - 1][j - 1][k + 2];}
                    if (k + 1 < N && j - 2 > -1) {state[i][j][k] += state[i - 1][j - 2][k + 1];}
                    if (k - 2 > -1 && j + 1 < N) {state[i][j][k] += state[i - 1][j + 1][k - 2];}
                    if (k - 1 > -1 && j + 2 < N) {state[i][j][k] += state[i - 1][j + 2][k - 1];}
                    if (k - 2 > -1 && j - 1 > -1) {state[i][j][k] += state[i - 1][j - 1][k - 2];}
                    if (k - 1 > -1 && j - 2 > -1) {state[i][j][k] += state[i - 1][j - 2][k - 1];}
                }
            }
        }
        return state[K][r][c] / pow(8, K);
    }

    int findPosition(int N, int r, int c) {
        int count = 0;
        if (c + 2 < N && r + 1 < N) {count++;}
        if (c + 1 < N && r + 2 < N) {count++;}
        if (c + 2 < N && r - 1 > -1) {count++;}
        if (c + 1 < N && r - 2 > -1) {count++;}
        if (c - 2 > -1 && r + 1 < N) {count++;}
        if (c - 1 > -1 && r + 2 < N) {count++;}
        if (c - 2 > -1 && r - 1 > -1) {count++;}
        if (c - 1 > -1 && r - 2 > -1) {count++;}
        return count;
    }
};  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值