原题链接在这里: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:

  • N will be between 1 and 25.
  • K will 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的更多相关文章

  1. LeetCode——688. Knight Probability in Chessboard

    一.题目链接:https://leetcode.com/problems/knight-probability-in-chessboard/ 二.题目大意: 给定一个N*N的棋盘和一个初始坐标值(r, ...

  2. leetcode 576. Out of Boundary Paths 、688. Knight Probability in Chessboard

    576. Out of Boundary Paths 给你一个棋盘,并放一个东西在一个起始位置,上.下.左.右移动,移动n次,一共有多少种可能移出这个棋盘 https://www.cnblogs.co ...

  3. 【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 ...

  4. 【LeetCode】688. Knight Probability in Chessboard 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/knight-pr ...

  5. 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 ...

  6. 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 ...

  7. [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 ...

  8. [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 ...

  9. Knight Probability in Chessboard

    2018-07-14 09:57:59 问题描述: 问题求解: 本题本质上是个挺模板的题目.本质是一个求最后每个落点的数目,用总的数目来除有所可能生成的可能性.这种计数的问题可以使用动态规划来进行解决 ...

随机推荐

  1. iframe与父窗口之间数据互相获取

    Js/Jquery获取iframe中的元素 博客分类: jquery javascript jquery  在web开发中,经常会用到iframe,难免会碰到需要在父窗口中使用iframe中的元素.或 ...

  2. C# XMLHttpRequest对象—Ajax实例

    Get: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> ...

  3. 纯代码编写qt登录界面(转)

    1. 新建Qt Widgets Application,项目名称为login1,在类信息页面保持类名和基类为MainWindow和QMainWindow不变,取消选择创建界面选项,如下图所示.     ...

  4. Shell编程之for和select循环

    一.for和select循环 1.for循环语法 for 变量名 in 变量取值列表 do 指令... done C语言型for循环 for ((exp1; exp2; exp3)) do 指令... ...

  5. Sublime Text3 打开文档乱码

    一.安装包管理器使用Ctrl+~快捷键或者通过View->Show Console菜单打开命令行,粘贴如下代码 import urllib.request,os; pf = 'Package C ...

  6. python局部变量引用问题

    a = [1, 2] b = 'Immutable' def test(): # global b print(a) a.append('asd') b = b + 'asd' # 当只是引用变量b的 ...

  7. 高通平台Bootloader启动流程【转】

    本文转载自:http://blog.csdn.net/fang_first/article/details/49615631 ====================基本知识============= ...

  8. 【P3957】跳房子(单调队列+DP+二分)

    终于把这个题缸出来了,话说这题也不是想的那么难... 因为最小的最大,所以二分,因为由前面推出后面,所以DP,因为输入单调,朴素DP会T,所以单调队列.要注意的是,这个题数据很大,要开LL,然后DP数 ...

  9. hive学习2(Navicat连接hive)

    Navicat连接hive 第一步:win下安装好mysql 第二步:win下安装Navicat 第三步:启动hadoop集群,启动hive 第四步:Navicat连接hive 在第四步中需先配置ss ...

  10. 关于 Java正则表达式中的Possessive数量修饰词的理解

    关于 Java正则表达式中的Possessive数量修饰词的理解 正则表达式对于数量限定符如 ?, + , *, {n, m} 的匹配默认是贪婪模式,比如: a.*b   匹配 acbab 的结果是 ...