LeetCode 688. Knight Probability in Chessboard “马”在棋盘上的概率 (C++/Java)
题目:
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:
Nwill be between 1 and 25.Kwill be between 0 and 100.- The knight always initially starts on the board.
分析:
给定一个N*N大小的棋盘,求走K步之后,马还停留在棋盘上的概率,规定马走出棋盘后就不再跳回来。
那么我们知道马可以向八个方向去跳,我们可以求出跳k步之后,在棋盘上停留的位置数,也就是k步后的情况个数,而每次跳8个方向,一共有8^K中情况,最后求比值就是概率。
通过dp二维数组保存前一步马所在的位置,遍历棋盘上每一个位置,再遍历八个方向,如果符合要求,即没有跳出棋盘,就将前一步所在位置的数量加到新的数组中,然后将新数组重新赋值给dp,继续下一步的遍历即可,最后返回位置数的和除以8^K便是答案。
程序:
C++
class Solution {
public:
double knightProbability(int N, int K, int r, int c) {
vector<vector<double>> dp(N, vector<double>(N, 0.0));
dp[r][c] = 1.0;
int steps[8][2] = {{1, 2}, {2, 1}, {2, -1}, {1, -2}, {-1, -2}, {-2, -1}, {-2, 1}, {-1, 2}};
for(int k = 0; k < K; ++k){
vector<vector<double>> temp(N, vector<double>(N, 0.0));
for(int i = 0; i < N; ++i){
for(int j = 0; j < N; ++j){
for(int l = 0; l < 8; ++l){
int x = i + steps[l][0];
int y = j + steps[l][1];
if(x < 0 || x >= N || y < 0 || y >= N)
continue;
temp[x][y] += dp[i][j];
}
}
}
swap(dp, temp);
}
double sum = 0.0;
for(int i = 0; i < N; ++i)
for(int j = 0; j < N; ++j)
sum += dp[i][j];
return sum / pow(8, K);
}
};
Java
class Solution {
public double knightProbability(int N, int K, int r, int c) {
double[][] dp = new double[N][N];
dp[r][c] = 1.0;
int[][] steps = new int[][]{{1, 2}, {2, 1}, {2, -1}, {1, -2}, {-1, -2}, {-2, -1}, {-2, 1}, {-1, 2}};
for(int k = 0; k < K; ++k){
double[][] temp = new double[N][N];
for(int i = 0; i < N; ++i){
for(int j = 0; j < N; ++j){
for(int l = 0; l < 8; ++l){
int x = i + steps[l][0];
int y = j + steps[l][1];
if(x < 0 || x >= N || y < 0 || y >= N)
continue;
temp[x][y] += dp[i][j];
}
}
}
dp = temp;
}
double sum = 0.0;
for(int i = 0; i < N; ++i)
for(int j = 0; j < N; ++j)
sum += dp[i][j];
return sum / Math.pow(8, K);
}
}
LeetCode 688. Knight Probability in Chessboard “马”在棋盘上的概率 (C++/Java)的更多相关文章
- 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 exa ...
- LeetCode 688. Knight Probability in Chessboard
原题链接在这里:https://leetcode.com/problems/knight-probability-in-chessboard/description/ 题目: On an NxN ch ...
- LeetCode——688. Knight Probability in Chessboard
一.题目链接:https://leetcode.com/problems/knight-probability-in-chessboard/ 二.题目大意: 给定一个N*N的棋盘和一个初始坐标值(r, ...
- leetcode 576. Out of Boundary Paths 、688. Knight Probability in Chessboard
576. Out of Boundary Paths 给你一个棋盘,并放一个东西在一个起始位置,上.下.左.右移动,移动n次,一共有多少种可能移出这个棋盘 https://www.cnblogs.co ...
- 【LeetCode】688. Knight Probability in Chessboard 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/knight-pr ...
- 【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 exa ...
- 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 ...
- Java实现 LeetCode 688 “马”在棋盘上的概率(DFS+记忆化搜索)
688. "马"在棋盘上的概率 已知一个 NxN 的国际象棋棋盘,棋盘的行号和列号都是从 0 开始.即最左上角的格子记为 (0, 0),最右下角的记为 (N-1, N-1). 现有 ...
- [LeetCode] 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 ...
- [Swift]LeetCode688. “马”在棋盘上的概率 | 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 ...
随机推荐
- 【NOIP2013模拟联考8】匹配(match) 题解
B 组都说看不懂--我也解释不清啊--只能写这么详细了 其实就是道板题 省流:f[i][s][j]表示字符串长度i,匹配情况s,ac自动机节点j Problem Description 给定k个字符串 ...
- 使用ollama + AnythingLLM快速且简单的在本地部署llm3
使用ollama + AnythingLLM快速且简单的在本地部署llm3 不多说,直接开始 一.安装ollama ollama官网:https://ollama.com/ 下载地址:https:// ...
- 如何将传统 Web 框架迁移部署到 Serverless 架构?
简介: 与其说 Serverless 架构是一个新的概念,不如说它是一种全新的思路,一种新的编程范式. 与其说 Serverless 架构是一个新的概念,不如说它是一种全新的思路,一种新的编程范式. ...
- 记一次 JMeter 压测 HTTPS 性能问题
简介:在使用 JMeter 压测时,发现同一后端服务,在单机 500 并发下,HTTP 和 HTTPS 协议压测 RT 差距非常大.同时观测后端服务各监控指标水位都很低,因此怀疑性能瓶颈在 JMet ...
- 漫画 | 一口气搞懂 Serverless !
简介: 第二届云原生编程挑战赛为热爱技术的年轻人提供一个挑战世界级技术问题的舞台,希望用技术为全社会创造更大价值. 作者 | 刘欣 呃,我可能是别人眼中所说的不用奋斗的一代. 大家喜欢听的什么多姿多 ...
- [FAQ] Windows 终端 git status 不识别文件名大小写的修改
当我们修改了文件名的大小写,git status 显示没有文件改动. 出现这种情况,首先看一下 git 的配置项是否忽略了文件问大小写: $ git config core.ignorecase ...
- Django之ajax简介
1.MTV与MVC 框架类型:MVC: M:models V:views C:controller Django用的框架就是MTV MTV: M:models T:templates V:views ...
- C语言程序设计-笔记3-循环结构
C语言程序设计-笔记3-循环结构 例4-1 用格雷戈里公式求给定精度的π值.使用格雷戈里公式求π的近似值,要求精确到最后一项的绝对值小于给定精度eps. =1--+-+- #include<s ...
- 防抖节流的含义使用场景及js实现原理
1.防抖:n 秒后在执行该事件,若在 n 秒内被重复触发,则重新计时.代码实现重在清零 clearTimeout. 应用:登录,提交,浏览器窗口的resizes事件,文本编辑保存 <script ...
- 学会使用 NumPy:基础、随机、ufunc 和练习测试
NumPy NumPy 是一个用于处理数组的 Python 库.它代表"Numerical Python". 基本 随机 ufunc 通过测验测试学习 检验您对 NumPy 的掌握 ...