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.

这道题给了我们一个大小为NxN国际象棋棋盘,上面有个骑士,相当于我们中国象棋中的马,能走‘日’字,给了我们一个起始位置,然后说允许我们走K步,问走完K步之后还能留在棋盘上的概率是多少。那么要求概率,我们必须要先分别求出分子和分母,其中分子是走完K步还在棋盘上的走法,分母是没有限制条件的总共的走法。那么分母最好算,每步走有8种跳法,那么K步就是8的K次方种了。关键是要求出分子,博主开始向的方法是以给定位置为起始点,然后进行BFS,每步遍历8种情况,遇到在棋盘上的就计数器加1,结果TLE了。上论坛看大家的解法,结果发现都是换了一个角度来解决问题的,并不很关心骑士的起始位置,而是把棋盘上所有位置上经过K步还留在棋盘上的走法总和都算出来,那么最后直接返回需要的值即可。跟之前那道Out of Boundary Paths没啥本质上的区别,又是换了一个马甲就不会了系列。还是要用DP来做,我们可以用三维DP数组,也可以用二维DP数组来做,这里为了省空间,我们就用二维DP数组来做,其中dp[i][j]表示在棋盘(i, j)位置上走完当前步骤还留在棋盘上的走法总和,初始化为1,我们其实将步骤这个维度当成了时间维度在不停更新。好,下面我们先写出8种‘日’字走法的位置的坐标,就像之前遍历迷宫上下左右四个方向坐标一样,这不过这次位置变了而已。然后我们一步一步来遍历,每一步都需要完整遍历一遍棋盘的每个位置,新建一个临时数组t,大小和dp数组相同,但是初始化为0,然后对于遍历到的棋盘上的每一个格子,我们都遍历8中解法,如果新的位置不在棋盘上了,直接跳过,否则就加上上一步中的dp数组中对应的值,遍历完棋盘后,将dp数组更新为这个临时数组t,参见代码如下:

解法一:

class Solution {
public:
double knightProbability(int N, int K, int r, int c) {
if (K == ) return ;
vector<vector<double>> dp(N, vector<double>(N, ));
vector<vector<int>> dirs{{-,-},{-,-},{-,},{-,},{,},{,},{,-},{,-}};
for (int m = ; m < K; ++m) {
vector<vector<double>> t(N, vector<double>(N, ));
for (int i = ; i < N; ++i) {
for (int j = ; j < N; ++j) {
for (auto dir : dirs) {
int x = i + dir[], y = j + dir[];
if (x < || x >= N || y < || y >= N) continue;
t[i][j] += dp[x][y];
}
}
}
dp = t;
}
return dp[r][c] / pow(, K);
}
};

我们也可以使用有memo数组优化的递归来做,避免重复运算,从而能通过OJ。递归下的memo数组其实就是迭代下的dp数组,这里我们用三维的数组,初始化为0。在递归函数中,如果k为0了,说明已经走了k步,返回 1。如果memo[k][r][c]不为0,说明这种情况之前已经计算过,直接返回。然后遍历8种走法,计算新的位置,如果不在棋盘上就跳过;然后更新memo[k][r][c],使其加上对新位置调用递归的返回值,注意此时带入k-1和新的位置,退出循环后返回memo[k][r][c]即可,参见代码如下:

解法二:

class Solution {
public:
vector<vector<int>> dirs{{-,-},{-,-},{-,},{-,},{,},{,},{,-},{,-}};
double knightProbability(int N, int K, int r, int c) {
vector<vector<vector<double>>> memo(K + , vector<vector<double>>(N, vector<double>(N, 0.0)));
return helper(memo, N, K, r, c) / pow(, K);
}
double helper(vector<vector<vector<double>>>& memo, int N, int k, int r, int c) {
if (k == ) return 1.0;
if (memo[k][r][c] != 0.0) return memo[k][r][c];
for (auto dir : dirs) {
int x = r + dir[], y = c + dir[];
if (x < || x >= N || y < || y >= N) continue;
memo[k][r][c] += helper(memo, N, k - , x, y);
}
return memo[k][r][c];
}
};

类似题目:

Out of Boundary Paths

参考资料:

https://discuss.leetcode.com/topic/105571/my-accepted-dp-solution

https://discuss.leetcode.com/topic/105597/c-java-dp-concise-solution

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Knight Probability in Chessboard 棋盘上骑士的可能性的更多相关文章

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

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

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

  3. LeetCode 688. Knight Probability in Chessboard

    原题链接在这里:https://leetcode.com/problems/knight-probability-in-chessboard/description/ 题目: On an NxN ch ...

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

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

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

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

  7. LeetCode——688. Knight Probability in Chessboard

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

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

  9. Knight Probability in Chessboard

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

随机推荐

  1. WPF学习笔记2

    XML语言中添加注释为<!---->,这是和C#不同的,但是和HTML十分相似. XAML是一种基于XML的标记语言,每一个XML元素代表.NET控件的一个对象,XML元素的属性可以是.N ...

  2. Jmeter-基于Ubuntu运行

    这几天折腾了很久,整合了一套接口自动化的持续集成工具,先从最基础的运行Jmeter说起.由于我是用Docker部署的持续集成环境,所以接口运行必须在服务器上 一:在Linux服务器先安装jdk 1:先 ...

  3. linux特殊字符及其作用

    1.通配符    ? 匹配单个字符    * 代表所有字符     [abcd] 匹配[]里任意一个字符.4选1 [a-d]    [!abcd]  匹配不含[]里任意一个字符的字符.[^abcd] ...

  4. 福州大学W班-alpha冲刺评分

    作业链接 https://edu.cnblogs.com/campus/fzu/FZUSoftwareEngineering1715W/homework/1159 作业要求 1.前期准备 阅读学习&l ...

  5. 福州大学W班-需求分析评分排名

    作业链接 https://edu.cnblogs.com/campus/fzu/FZUSoftwareEngineering1715W/homework/1019 作业要求 1.需求文档 1) 参考& ...

  6. 实验三《Java面向对象程序设计》实验报告

    20162308 实验三<Java面向对象程序设计>实验报告 实验内容 XP基础 XP核心实践 IDEA工具学习 密码学算法基础 实验步骤 (一)Refactor/Reformat使用 p ...

  7. bug终结者 团队作业第四、五周

    bug终结者 团队作业第四.五周 博客编辑者:20162322朱娅霖 本周学习任务: 团队协作完成<需求规格说明书> 工作流程 第四周 团队成员各自完成蓝墨云班课中<需求规格说明书& ...

  8. requestAnimationFrame Web中写动画的另一种选择

    HTML5和CSS3盛行的今天  动画变得很简单实现 我们可以用transition . animation + keyframe .也可以用canvas等 我在上一篇 点击回到顶部的文章中发现的这个 ...

  9. javascript原型链__proto__属性的理解

    在javascript中,按照惯例,构造函数始终都应该以一个大写字母开头,而非构造函数则应该以一个小写字母开头.一个方法使用new操作符创建,例如下面代码块中的Person1(可以吧Person1看做 ...

  10. 为什么 asnyc await 可以提高web程序的吞吐量

    (转网上一段话) Web程序天生就是多线程的,且web线程都是跑的线程池线程(使用线程池线程是为了避免不断创建.销毁线程所造成的资源成本浪费),而线程池线程可使用线程数量是一定的,尽管可以设置,但它还 ...