LeetCode 688. Knight Probability in Chessboard “马”在棋盘上的概率 (C++/Java)
题目:
On an N
xN
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.
分析:
给定一个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 ...
随机推荐
- 如何基于Django中的WebSockets和异步视图来实现实时通信功能
本文分享自华为云社区<结合Django中的WebSockets和异步视图实现实时通信功能的完整指南>,作者: 柠檬味拥抱. 在现代Web应用程序中,实时通信已经成为了必不可少的功能之一.无 ...
- Serverless 在阿里云函数计算中的实践
简介: 近日,阿里云 aPaaS&Serverless 前端技术专家袁坤在 CSDN 云原生 meetup 长沙站分享了 Serverless 在阿里云函数计算 FC 的实践. 作者:CSDN ...
- 消息队列Kafka「检索组件」重磅上线!
简介:本文对消息队列 Kafka「检索组件」进行详细介绍,首先通过对消息队列使用过程中的痛点问题进行介绍,然后针对痛点问题提出相应的解决办法,并对关键技术技术进行解读,旨在帮助大家对消息队列 Kaf ...
- 基于 KubeVela 与 Kubernetes 打造“无限能力”的开放 PaaS
简介: 本文整理自阿里云容器技术专家.OAM 规范主要制定者之一.KubeVela 作者和负责人孙健波(天元)在阿里云开发者社区"周二开源日"的直播分享,将剖析当前 Kuberne ...
- 深入分析 Flutter 渲染性能
简介: Flutter 有很多优点,特别是对于开发者来说,跨平台多端支持,丰富的 UI 组件库和交互效果,声明式 UI,React 的更新方式,Hot-reload 提高开发效率等等.虽然它在渲染性能 ...
- 一文了解EPaxos核心协议流程
简介: EPaxos(Egalitarian Paxos)作为工业界备受瞩目的下一代分布式一致性算法,具有广阔的应用前景.但纵观业内,至今仍未出现一个EPaxos的工程实现,甚至都没看到一篇能把EPa ...
- Morphling:云原生部署 AI , 如何把降本做到极致?
简介: Morphling 本意是游戏 Dota 中的英雄"水人",他可以根据环境要求,通过灵活改变自身形态,优化战斗表现.我们希望通过 Morphling 项目,实现针对机器学 ...
- [PPT] WPS 提取 PPT 中的母版到另一份 PPT 中
1. 打开 PPT. 2. 视图 - 幻灯片母版,在第一个 ppt 上面 "鼠标右键 - 复制" 来进行拷贝. 3. 打开目标 PPT,视图 - 幻灯片模板,快捷键 Ctrl + ...
- Git 版本控制:构建高效协作和开发流程的最佳实践
引言 版本控制是开发中不可或缺的一部分,他允许多人同时协作,通过记录每一次代码的变更,帮助开发者理解何时.为什么以及谁做了修改.这不仅有助于错误追踪和功能回溯,还使得团队能够并行工作,通过分支管理实现 ...
- dotnet C# 使用 Vortice 支持 Direct2D1 离屏渲染
本文告诉大家如何使用 Vortice 进行 D2D 的离屏渲染功能,本文将在一个纯控制台无窗口的应用下,使用 Direct2D1 进行离屏绘制,将绘制结果保存为本地图片文件 本文属于使用 Vortic ...