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 ...
随机推荐
- JavaScript中数值小知识
1. 数值10.0 这种类似的会被去掉数值后的0 之所以这样是因为,整数的存储空间占用比浮点数小,当一个数值不是真浮点数(即10.0这种格式),会被转换为整数10,如果业务中有一些需求需要进行数值位数 ...
- 登录chatgpt的时候出现429的解决方法,亲测有效
登录chatgpt的时候出现429的解决方法 PS:在2023年3月14日晚还是可以用的,亲测有效 登录chatgpt的时候出现429的解决方法 很多时候在国内用代理进入chatgpt的时候会出现42 ...
- 关于<property name="hibernate.hbm2ddl.auto"></property>中的参数填写
hibernate的数据库表自动生成参数 关于<property name="hibernate.hbm2ddl.auto"></property>中的参数 ...
- 剑指offer56(Java)-数组中出现的次数Ⅰ(中等)
题目: 一个整型数组 nums 里除两个数字之外,其他数字都出现了两次.请写程序找出这两个只出现一次的数字.要求时间复杂度是O(n),空间复杂度是O(1). 示例 1: 输入:nums = [4,1, ...
- Apsara Stack 同行者专刊 | 政企混合云技术架构的演进和发展
简介: 现在,政企客户已进入到用云计算全面替换传统IT基础架构的攻坚阶段,混合云与传统架构的技术产品能力也正在经历全面的比较与评估.阿里云混合云平台首席架构师张晓丹分享IT架构技术深刻洞察,并对政企混 ...
- Timing:在线自习室快速搭建
通过超低延迟的音视频通信技术.视频连麦.弱网传输算法,快速搭建自习场景,提升自习效率. 客户简介 氪细胞主打产品Timing,是国内最早推出,也是规模最大的在线自习室,是新一代的教育与社交融合平 ...
- Flink 1.12 资源管理新特性回顾
简介: 介绍 Flink 1.12 资源管理的一些特性,包括内存管理.资源调度.扩展资源框架. 本文由社区志愿者陈政羽整理,Apache Flink Committer.阿里巴巴技术专家宋辛童,Apa ...
- [FE] nvm-windows: Microsoft/NPM/Google 推荐 Windows 的 Node.js 版本管理器, posix 的 nvm-sh/nvm
1. 到 github 下载 nvm-setup.zip 并安装. Releases · coreybutler/nvm-windows (github.com) 2. 安装一个版本的 nodejs. ...
- WPF 已知问题 Separator 无法应用 ContextMenu 定义的默认样式
本文记录一个 WPF 已知问题,在 ContextMenu 的 Resources 里定义 Separator 的默认样式,在 ContextMenu 里面的 Separator 将应用不上,或者说不 ...
- 2019-11-29-VisualStudio-2019-如何离线下载
title author date CreateTime categories VisualStudio 2019 如何离线下载 lindexi 2019-11-29 08:38:13 +0800 2 ...