Leetcode 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.

解法:

这个题是要我们算出 knight 在随机走K步之后留在棋盘上的概率 P
P=可以留在棋盘上的走法总的走法P = \frac{可以留在棋盘上的走法}{总的走法} P=总的走法可以留在棋盘上的走法​
knight 每次都会向八个方向走,所以我们可以推出:走K步 -> 可以有 8^K 种走法;

那么接下来就需要考虑可以留在棋盘上的走法怎么计算了

在求这种情况时候,我们可以看作是求解 迷宫问题 ,大致思路就是,每次我都让knight都可以向八个方向移动,但是如果有跳出棋盘的走法,就舍弃这种走法,最后只保留下knight不跳出棋盘的走法,随后在前一步的基础上继续进行向八个方向移动的操作,迭代 K步后,最后把这些情况汇总起来,就是满足要求的解。

这个题思路比较直观,但是实现的代码需要嵌套循环多一点,所以上面的这种做法效率会比较低一点。


代码:

class Solution {public:double knightProbability(int N, int K, int r, int c) {if (K == 0) return 1;vector<vector<double>> dp(N, vector<double>(N, 1));vector<vector<int>> dirs{{-1,-2},{-2,-1},{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2}};for (int m = 0; m < K; ++m) {vector<vector<double>> t(N, vector<double>(N, 0));for (int i = 0; i < N; ++i) {for (int j = 0; j < N; ++j) {for (auto dir : dirs) {int x = i + dir[0], y = j + dir[1];if (x < 0 || x >= N || y < 0 || y >= N) continue;t[i][j] += dp[x][y];}}}dp = t;}return dp[r][c] / pow(8, K);}
};

Leetcode 688.Knight Probability in Chessboard相关推荐

  1. leetcode 688. Knight Probability in Chessboard | 688. “马”在棋盘上的概率(dp,记忆化搜索)

    题目 https://leetcode.com/problems/knight-probability-in-chessboard/ 题解 左神讲过类似问题: 给定5个参数,N,M,row,col,k ...

  2. LWC 52:688. Knight Probability in Chessboard

    LWC 52:688. Knight Probability in Chessboard 传送门:688. Knight Probability in Chessboard Problem: On a ...

  3. Knight Probability in Chessboard

    Knight Probability in Chessboard 题目描述:在NxN的棋盘上,骑士走"日"字,经过K次行走之后,骑士还留在棋盘上的概率是多少? 骑士在棋盘上行走时, ...

  4. Knight On the Chessboard

    Leetcode 688: 骑士在棋盘上的概率 Definition: 思路 由于刷题尹始对DP和DFS之类的问题还没有什么概念,看到题目第一想法就是直接正向求解,在使用递归实现基本问题的求解之后发现 ...

  5. leetcode - 688. “马”在棋盘上的概率

    688. "马"在棋盘上的概率 -------------------------------------------- 已知一个 NxN 的国际象棋棋盘,棋盘的行号和列号都是从 ...

  6. LeetCode 688. “马”在棋盘上的概率(DP)

    文章目录 1. 题目 2. 解题 1. 题目 已知一个 NxN 的国际象棋棋盘,棋盘的行号和列号都是从 0 开始.即最左上角的格子记为 (0, 0),最右下角的记为 (N-1, N-1). 现有一个 ...

  7. LeetCode 688. “马”在棋盘上的概率

    688. "马"在棋盘上的概率 已知一个 NxN 的国际象棋棋盘,棋盘的行号和列号都是从 0 开始.即最左上角的格子记为 (0, 0),最右下角的记为 (N-1, N-1). 现有 ...

  8. 【LeetCode】935. Knight Dialer 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划TLE 空间换时间,利用对称性 优化空间复杂 ...

  9. LeetCode All in One 题目讲解汇总(持续更新中...)

    原文地址:https://www.cnblogs.com/grandyang/p/4606334.html 终于将LeetCode的大部分题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开 ...

最新文章

  1. 编程能力差,学不好Python、AI、Java等技术,90%是输在了这点上!
  2. SafeSEH原理与对抗
  3. Java虚拟机-内存分配策略
  4. Python大神告诉你,学习Python应该读哪些书!
  5. 线性表之顺序存储结构相关算法学习
  6. 提高WordPress访问速度与性能的技巧总结
  7. Spinlock 简介(转)
  8. 20154319 实验九web安全基础实践
  9. C# vs MySql
  10. kali安装步骤失败 选择并安装软件_交通仿真建模软件Vissim7.0/6.0/5.3安装步骤
  11. C++ 产生0-1之间的随机数
  12. Unity3D Shader 新手教程(2/6) —— 积雪Shader
  13. 安装mysql报msvcr100_解决安装mysql 提示msvcr100.dill 丢失,的最快方法
  14. 基于linux的oracle_rac实时应用集群研究,基于Linux的OracleRAC实时应用集群研究毕业设计论文...
  15. htmlVideo禁止拖动进度条禁止快进
  16. os.listdir('.')
  17. 错误 1 error LNK1107: 文件无效或损坏: 无法在 0x320 处读取的————解决方案
  18. KindEditor 自定义插件
  19. wegame与客户端服务器连接中断95,DNF无限网络异常09解决方法 使用wegame游戏进不去连接中断...
  20. 计算机网络知识的PPT,计算机网络基础理论知识.ppt

热门文章

  1. MySQL的索引和存储引擎
  2. 最小二乘法多项式曲线拟合原理与实现(错误地方已经修改底层补充自己写的java实现)
  3. 复变函数与积分变换 期末复习 思维导图
  4. 寓言故事《听风就是雨的狼》
  5. 计算机学院心理节活动,计算机科学与技术学院心理团体辅导活动圆满举办
  6. 在vscode中调用dosbox来运行tasm汇编程序(tasks.json code runner)
  7. Java获取当前年份、月份和日期
  8. 一位资深程序员大牛给予Java学习者的学习路线建议
  9. Steve Jobs Ready To Retake Apple's Helm - Sources
  10. python心得1000字-python学习心得(一)