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 ...
随机推荐
- 【oracle】想要得到一个与输入顺序相同的结果
[oracle]想要得到一个与输入顺序相同的结果 在Oracle中,输出结果顺序好像是个rowid相同的,也就是经常使用的rownum序列的值,所以可以通过对rownum进行order by来让输出结 ...
- 第12課-Mirth生产环境宕机后基于服务配置XML备份恢复之记录
Mirth Connect作为集成交换平台,生产环境互联互通了众多系统,脑残的是连自家关键业务系统都依托mirth来进行交互,宕机或故障对身处其中的一次紧张的业务系统升级都造成高度的精神紧张:这种宕机 ...
- 一文搞懂传统单节点网站的 Serverless 上云
简介: 阿里云函数计算 FC 是事件驱动的全托管计算服务,真正的无需去考虑服务器的运维管理,只需要完成开发的代码进行上传,函数计算会通过角色策略去规划计算资源,弹性的方式执行函数,最后高效的执行部署. ...
- 先行一步,7大技术创新和突破,阿里云把 Serverless 领域的这些难题都给解了
简介: 函数计算 FC 首创 GPU 实例.业内首发实例级别可观测和调试.率先提供端云联调和多环境部署能力.GB 级别镜像启动时间优化至秒级.VPC 网络建连优化至200ms,Serverless ...
- [GPT] 监测输入框被 js 设置了值 ?input 输入框被设置了 value 值,但是没有触发 change 事件?
1. input 输入框被设置了 value 值,但是没有触发 change 事件 ? 如果输入框的 value 值是通过 JavaScript 代码直接设置的,那么不会触发 change 事件,这是 ...
- [FAQ] WebStorm/PHPStorm:设置 HTML/JavaScript/PHP 注释缩进行为,代码片段
[注释行为] Preferences -> Code Style 选择语言后,找到 Wrapping and Braces, 取消 Comment at first column. 如果是HTM ...
- dotnet 读 WPF 源代码 聊聊 DispatcherTimer 的实现
本文来告诉大家在 WPF 框架里面,是如何实现 DispatcherTimer 的功能.有小伙伴告诉我,读源代码系列的博客看不动,原因是太底层了.我尝试换一个方式切入逻辑,通过提问题和解决问题的方法, ...
- 2019-9-30-dotnet-枚举当前设备wifi热点
title author date CreateTime categories dotnet 枚举当前设备wifi热点 lindexi 2019-09-30 14:42:18 +0800 2019-9 ...
- STM32中的看门狗
先复制一段度娘的东西: 在由单片机构成的微型计算机系统中,由于单片机的工作常常会受到来自外界电磁场的干扰,造成程序的跑飞,而陷入死循环,程序的正常运行被打断,由单片机控制的系统无法继续工作,会造成整个 ...
- 006_Orcad创建Hetergeneous分裂元件
006_Orcad创建Hetergeneous分裂元件 以169脚的EMMC为例: 分为两部分,用到的引脚和NC的引脚. 先画一个框,依据引脚功能添加引脚.A部分做好,做B部分.引脚多,可以用pin ...