LeetCode 688. Knight Probability in Chessboard
原题链接在这里:https://leetcode.com/problems/knight-probability-in-chessboard/description/
题目:
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.
题解:
类似Out of Boundary Paths.
DP问题. 求最后在board上的概率. 反过来想,走完K步棋子在board上的哪个位置呢. 反过来走, 看board上所有位置走完K步后能到初始位置(r,c)的数目和.
储存历史信息是走到当前这步时棋盘上能走到每个位置的不同走法.
递推时, 向所有方向移动, 若是还在board上就把自己的走法加到新位置的走法上.
初始化所有位置只有1种走法.
答案K步之后到初始位置的走法除以Math.pow(8,K).
Time Complexity: O(K*N^2).
Space: O(N^2).
AC Java:
class Solution {
public double knightProbability(int N, int K, int r, int c) {
int [][] moves = {{1,2},{1,-2},{2,1},{2,-1},{-1,2},{-1,-2},{-2,1},{-2,-1}};
double [][] dp0 = new double[N][N];
for(double [] row : dp0){
Arrays.fill(row, 1);
}
for(int step = 0; step<K; step++){
double [][] dp1 = new double[N][N];
for(int i = 0; i<N; i++){
for(int j = 0; j<N; j++){
for(int [] move : moves){
int row = i + move[0];
int col = j + move[1];
if(isIllegal(row, col, N)){
dp1[row][col] += dp0[i][j];
}
}
}
}
dp0 = dp1;
}
return dp0[r][c]/Math.pow(8,K);
}
private boolean isIllegal(int row, int col, int len){
return row>=0 && row<len && col>=0 && col<len;
}
}
LeetCode 688. Knight Probability in Chessboard的更多相关文章
- 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
题目如下: 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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/knight-pr ...
- 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 ...
- [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 ...
- Knight Probability in Chessboard
2018-07-14 09:57:59 问题描述: 问题求解: 本题本质上是个挺模板的题目.本质是一个求最后每个落点的数目,用总的数目来除有所可能生成的可能性.这种计数的问题可以使用动态规划来进行解决 ...
随机推荐
- loadrunner之脚本篇——代理录制
版本:Loadruner 11.0 A.PC端录制Web应用程序 步骤1:根据实际情况,选择对应的协议 本例中选择Web(HTTP/HTML),如下 步骤2:找到代理设置界面 点击 Start Rec ...
- 对JAVA的集合的理解
对JAVA的集合的理解是相对于数组 1.数组是大小固定的,并且同一个数组只能存放类型一样的数据(基本类型/引用类型) 2.JAVA集合可以存储和操作数目不固定的一组数据. 3.所有的JAVA集合都位 ...
- OpenGL学习进程(6)第四课:点、边和图形(一)点
本节是OpenGL学习的第四个课时,下面介绍OpenGL点的相关知识: (1)点的概念: 数学上的点,只有位置,没有大小.但在计算机中,无论计算精度如何提高,始终不能表示一个无穷小的点 ...
- ios-如何搭建IPv6网络测试环境(转)
工具/原料 mac一台 iPhone手机2台(一台用于测试,另一台提供网络) 方法/步骤 准备网络.通过数据线连接iPhone和Mac,并将iPhone手机连接的Wi-Fi关闭,使用自己的 ...
- Oracle常用的OCI函数
一. Oracle oci工具包安装: $ORACLE_HOME\BIN:执行文件和help文件 $ORACLE_HOME\OCI\INCLUDE:头文件 $ORACLE_HOME\OCI\LIB\B ...
- java web数据库连接封装-simple
package cn.cslg.bm.web.util; import java.sql.Connection; import java.sql.DriverManager; import org.a ...
- INSPIRED启示录 读书笔记 - 第28章 创业型公司的产品管理
产品设计方式 第一步:创业初期只设三个职位,产品经理.交互设计师和原型开发人员(职位可以兼任) 第二步:快速展开产品设计(高保真原型),邀请真实的目标用户验证产品原型,迭代修改 第三步:随着迭代的深入 ...
- INSPIRED启示录 读书笔记 - 第1章 关键角色及其职责
现代软件产品团队 1.产品经理的主要职责分为两项:评估产品机会和定义要开发的产品 2.用户体验设计师(由多种角色组成,这里面最关键的是交互设计师) 交互设计师负责深入理解目标用户,设计有价值 ...
- Go sqlx库
sqlx is a library which provides a set of extensions on go's standard database/sql library. sqlx sup ...
- Centos6.8安装Mysql5.7
1.下载 wget https://dev.mysql.com/get/mysql57-community-release-el6-9.noarch.rpm 2.安装用来配置mysql的yum源的rp ...