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 问题描述: 问题求解: 本题本质上是个挺模板的题目.本质是一个求最后每个落点的数目,用总的数目来除有所可能生成的可能性.这种计数的问题可以使用动态规划来进行解决 ...
随机推荐
- js hash
1)新建hash hash= { name : "image", "number" : &q ...
- linux命令详解之df(6/19)
df命令作用是列出文件系统的整体磁盘空间使用情况.可以用来查看磁盘已被使用多少空间和还剩余多少空间. df命令显示系统中包含每个文件名参数的磁盘使用情况,如果没有文件名参数,则显示所有当前已挂载文件系 ...
- 常用模块-----configparser & subprocess
configparser 模块 功能:操作模块类的文件,configparser类型文件的操作类似于字典,大多数用法和字典相同. 新建文件: import configparser cfg=confi ...
- Shell编程之循环控制及状态返回值
1.break.continue.exit.return的对比 break.continue在条件语句和循环语句中用于控制程序走向: exit用于终止所有语句并退出当前脚本,还可以返回上一次程序或命令 ...
- R的几个基础函数
本章目录: 1.路径和文件 2.数据转换 3.获得帮助 路径和文件: 1.工作路径: 显示当前路径:getwd() 设置路径:setwd(“绝对路径”) 2.目录: 创建目录:dir.create(& ...
- mongodb 中 Aggregation 的管道和分片集合( Pipeline and Sharded Collections)
mongodb 中的aggretion 中,如果管道中存在一个与之相匹配的shard key ,那么这个管道只运行在与之相匹配的shard 中,在以前(3.2),pipeline 被分流,最后又由pr ...
- relativePath
比如: com.tenace tenace 2.0.1 ../pom.xml //刚开始无此句 com.spider engine 2.6.0-SNAPSHOT tenace作为pom项目已经发布至r ...
- linux下java unrecognized class file version错误的解决
root@Mr javaPC]# java HelloWorldException in thread “main” java.lang.ClassFormatError: HelloWorld (u ...
- ZooKeeper服务-数据模型
ZooKeeper是一个具有高可用性的高性能协调服务. 数据模型 ZooKeeper维护着一个树形层次结构,树中的节点被称为znode.Znode可以用于存储数据,并且有一个与之相关联的ACL(Acc ...
- ViewPagerAdapter
) { ; } } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == arg1; } ...