原题链接在这里: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. 面对 to B 业务该如何构建研发管理体系?

    未来离我们越来越近,而过去并未走远,我们发现科技公司2B业务兴起,腾讯认为互联网下半场属于产业互联网,需要进行一次重要的战略升级.它们在国庆节最后一天进行新一轮组织架构调整,最亮眼的就是新成立云与智慧 ...

  2. comet4j开发指南

    http://blog.csdn.net/majian_1987/article/details/8489738 好多朋友反映,因排版问题,文章部分边缘内容无法查看.尝试过排版,但仍无法显示正常,所以 ...

  3. nodejs数据接收body-parser中间件

    给大家翻译一下npm上body-parser的资料 nodejs 的body数据解析中间件 插件作用:对于req.body属性,在操作数据前分析进来的请求体的插件 首先学习解析一个http处理 这篇文 ...

  4. Django详解之四、cookie和session

    一.使用背景 思路 简单的后台管理:对人员的管理 1. 登录注册 2. 老师 班级管理 学院管理 3. 增删改查 开发: 1. 定义数据库表结构 a) 表结构关系 i. class classes(m ...

  5. 用java.lang.Math.random()语句,随机输出{size:自定义参数}个数不重复并且按顺序从小到大排列(冒泡排序)

    package com.test; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.lan ...

  6. LeetCode——sum-root-to-leaf-numbers

    Question Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a ...

  7. js提示确认删除吗

    <script language="javascript"> function delcfm() { if (!confirm("确认要删除?")) ...

  8. php二维数组自定义排序

    $arr = array( '0' => array('id' =>1,'price'=>200), '1' => array('id' =>2,'price'=> ...

  9. 由于ptrace.h文件导致的内核编译出错的解决方法

    arch/x86/kernel/ptrace.c:1472:17: error: conflicting types for 'syscall_trace_enter'  In file includ ...

  10. Hadoop- Namenode经常挂掉 IPC's epoch 9 is less than the last promised epoch 10

    如题出现Namenode经常挂掉 IPC's epoch 9 is less than the last promised epoch 10, 2019-01-03 05:36:14,774 INFO ...