原题链接在这里: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. google黑客语法总结

    搜索也是一门艺术 说起Google,可谓无人不知无人不晓,其强大的搜索功能,可以让你在瞬间找到你想要的一切.不过对于普通的用户而言,Google是一个强大的搜索引擎:而对于黑客而言,则可能是一款绝佳的 ...

  2. iOS代码瘦身实践

    1 分析当前ipa的组成 一般一个ipa会包含: 1) 资源文件 本地文件:数据.配置.数据库等等 字体文件 图片资源 2)  源代码 通过生成linkmap文件,分析源代码生成的编译文件的大小.在B ...

  3. Springboot WebSocket例子

    Springboot整合WebSocket 1.application.properties #设置服务端口号 server.port=8080 #thymeleaf配置 #是否启用模板缓存. spr ...

  4. Mybatis一对多/多对多查询时只查出了一条数据

    问题描述: 如果三表(包括了关系表)级联查询,主表和明细表的主键都是id的话,明细表的多条数据只能查询出来第一条/最后一条数据. 三个表,权限表(Permission),权限组表(Permission ...

  5. Shell编程之IF条件

    一.if条件语句的知识与实践 1.if条件语句语法(单分支结构) 第一种: if < 条件表达式 > then 指令 fi 第二种: if < 条件表达式 >; then 指令 ...

  6. samtools使用过程中出现的问题

    1.EOP marker is absent 在使用samtools index时出现 EOF是指the end of file,即samtools认为你的bam文件是不完整的. 如果把view参数的 ...

  7. Could not reserve enough space for object heap解决办法

    Centos6.4  Jdk1.6 1.在终端输入Java命令报错 [root@localhost local]# java Error occurred during initialization ...

  8. HBase协处理器的使用(添加Solr二级索引)

    给HBase添加一二级索引,HBase协处理器结合solr 代码如下 package com.hbase.coprocessor; import java.io.IOException; import ...

  9. ZooKeeper学习第八期---ZooKeeper伸缩性

    转:http://www.cnblogs.com/sunddenly/p/4143306.html 一.ZooKeeper中Observer 1.1 ZooKeeper角色 经过前面的介绍,我想大家都 ...

  10. HDU 5884 Sort(2016年青岛网络赛 G 二分+贪心+小优化)

    好题 题意:给你n<=100000个数,每个数范围[0,1000],然后给你一个最大的代价T,每次最多合并k个数成为一个数,代价为k个数的总和.问最后合成1个数的总代价不大于T的最小k 题解:我 ...