[Leetcode] 688. Knight Probability in Chessboard 解题报告

探讨在一个NxN的棋盘上,骑士从指定位置出发,在随机选择八个可能的方向移动K次后仍留在棋盘上的概率。通过递推算法计算每一步后骑士位于棋盘各位置的概率,并最终汇总得到骑士停留在棋盘上的总概率。

题目

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.

    思路

    我们定义一个映射map<pos, prob> status,将位置映射到其出现的概率上。对于每个pos而言,它可以到达8个不同的位置。而到这8个位置的概率都是status[pos] * 1 / 8。我们分别计算这8个位置,如果发现它还在棋盘的范围之内,就将其概率增加status[pos] * 1 / 8。当行走K步之后,我们将status中的所有概率加起来,即为knight最后留在棋盘上的概率。

    在实现中,为了避免开辟过大的空间以及频繁的赋值操作引起效率降低,我们定义status_even和status_odd,用来交替更新数据。这样算法的时间复杂度就是O(KN^2),空间复杂度可以降低到O(N^2)。

    代码

    class Solution {
    public:
        double knightProbability(int N, int K, int r, int c) {
            map<vector<int>, double> status_even, status_odd;
            status_even[{r, c}] = 1.0;
            for (int i = 0; i < K; ++i) {
                if (i % 2 == 0) {
                    status_odd.clear();
                    for (auto it = status_even.begin(); it != status_even.end(); ++it) {
                        movePositions(N, it->first[0], it->first[1], it->second, status_odd);
                    }
                }
                else {
                    status_even.clear();
                    for (auto it = status_odd.begin(); it != status_odd.end(); ++it) {
                        movePositions(N, it->first[0], it->first[1], it->second, status_even);
                    }
                }
            }
            map<vector<int>, double>::iterator begin = K % 2 == 0 ? status_even.begin() : status_odd.begin();
            map<vector<int>, double>::iterator end = K % 2 == 0 ? status_even.end() : status_odd.end();
            double ret = 0;
            for (auto it = begin; it != end; ++it) {
                ret += it->second;
            }
            return ret;
        }
    private:
        void movePositions(int N, int r, int c, double value, map<vector<int>, double> &positions) {
            vector<vector<int>> next_pos;
            if (r + 2 < N) {
                if (c + 1 < N) {
                    next_pos.push_back({r + 2, c + 1});
                }
                if (c - 1 >= 0) {
                    next_pos.push_back({r + 2, c - 1});
                }
            }
            if (r - 2 >= 0) {
                if (c + 1 < N) {
                    next_pos.push_back({r - 2, c + 1});
                }
                if (c - 1 >= 0) {
                    next_pos.push_back({r - 2, c - 1});
                }
            }
            if (r + 1 < N) {
                if (c + 2 < N) {
                    next_pos.push_back({r + 1, c + 2});
                }
                if (c - 2 >= 0) {
                    next_pos.push_back({r + 1, c - 2});
                }
            }
            if (r - 1 >= 0) {
                if (c + 2 < N) {
                    next_pos.push_back({r - 1, c + 2});
                }
                if (c - 2 >= 0) {
                    next_pos.push_back({r - 1, c - 2});
                }
            }
            for (auto &p : next_pos) {
                positions[p] += value / 8.0;
            }
        }
    };
内容概要:本文提出了一种基于融合鱼鹰算法和柯西变异的改进麻雀优化算法(OCSSA),用于优化变分模态分解(VMD)的参数,进而结合卷积神经网络(CNN)与双向长短期记忆网络(BiLSTM)构建OCSSA-VMD-CNN-BILSTM模型,实现对轴承故障的高【轴承故障诊断】基于融合鱼鹰和柯西变异的麻雀优化算法OCSSA-VMD-CNN-BILSTM轴承诊断研究【西储大学数据】(Matlab代码实现)精度诊断。研究采用西储大学公开的轴承故障数据集进行实验验证,通过优化VMD的模态数和惩罚因子,有效提升了信号分解的准确性与稳定性,随后利用CNN提取故障特征,BiLSTM捕捉时间序列的深层依赖关系,最终实现故障类型的智能识别。该方法在提升故障诊断精度与鲁棒性方面表现出优越性能。; 适合人群:具备一定信号处理、机器学习基础,从事机械故障诊断、智能运维、工业大数据分析等相关领域的研究生、科研人员及工程技术人员。; 使用场景及目标:①解决传统VMD参数依赖人工经验选取的问题,实现参数自适应优化;②提升复杂工况下滚动轴承早期故障的识别准确率;③为智能制造与预测性维护提供可靠的技术支持。; 阅读建议:建议读者结合Matlab代码实现过程,深入理解OCSSA优化机制、VMD信号分解流程以及CNN-BiLSTM网络架构的设计逻辑,重点关注参数优化与故障分类的联动关系,并可通过更换数据集进一步验证模型泛化能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值