From 02915f99d08f04b50df812b65f440d333a98faf2 Mon Sep 17 00:00:00 2001 From: AC_Oier Date: Thu, 17 Feb 2022 09:08:02 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E2=9C=A8update:=20Modify=20877?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "Index/\345\214\272\351\227\264 DP.md" | 2 +- ...\210\217\357\274\210\344\270\255\347\255\211\357\274\211.md" | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git "a/Index/\345\214\272\351\227\264 DP.md" "b/Index/\345\214\272\351\227\264 DP.md" index ddeb2bb4..82e266e9 100644 --- "a/Index/\345\214\272\351\227\264 DP.md" +++ "b/Index/\345\214\272\351\227\264 DP.md" @@ -3,6 +3,6 @@ | [87. 扰乱字符串](https://leetcode-cn.com/problems/scramble-string/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/scramble-string/solution/gong-shui-san-xie-yi-ti-san-jie-di-gui-j-hybk/) | 困难 | 🤩🤩🤩 | | [375. 猜数字大小 II](https://leetcode-cn.com/problems/guess-number-higher-or-lower-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/guess-number-higher-or-lower-ii/solution/gong-shui-san-xie-yi-ti-shuang-jie-ji-yi-92e5/) | 中等 | 🤩🤩🤩🤩🤩 | | [516. 最长回文子序列](https://leetcode-cn.com/problems/longest-palindromic-subsequence/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/longest-palindromic-subsequence/solution/gong-shui-san-xie-qu-jian-dp-qiu-jie-zui-h2ya/) | 困难 | 🤩🤩🤩 | -| [664. 奇怪的打印机](https://leetcode-cn.com/problems/strange-printer/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/strange-printer/solution/gong-shui-san-xie-noxiang-xin-ke-xue-xi-xqeo9/) | 中等 | 🤩🤩🤩🤩🤩 | +| [664. 奇怪的打印机](https://leetcode-cn.com/problems/strange-printer/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/strange-printer/solution/gong-shui-san-xie-noxiang-xin-ke-xue-xi-xqeo9/) | 困难 | 🤩🤩🤩🤩🤩 | | [877. 石子游戏](https://leetcode-cn.com/problems/stone-game/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/stone-game/solution/gong-shui-san-xie-jing-dian-qu-jian-dp-j-wn31/) | 中等 | 🤩🤩🤩🤩 | diff --git "a/LeetCode/871-880/877. \347\237\263\345\255\220\346\270\270\346\210\217\357\274\210\344\270\255\347\255\211\357\274\211.md" "b/LeetCode/871-880/877. \347\237\263\345\255\220\346\270\270\346\210\217\357\274\210\344\270\255\347\255\211\357\274\211.md" index 35796467..ea544681 100644 --- "a/LeetCode/871-880/877. \347\237\263\345\255\220\346\270\270\346\210\217\357\274\210\344\270\255\347\255\211\357\274\211.md" +++ "b/LeetCode/871-880/877. \347\237\263\345\255\220\346\270\270\346\210\217\357\274\210\344\270\255\347\255\211\357\274\211.md" @@ -120,7 +120,7 @@ class Solution { ### 最后 -这是我们「刷穿 LeetCode」系列文章的第 `No.877` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先将所有不带锁的题目刷完。 +这是我们「刷穿 LeetCode」系列文章的第 `No.877` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。 在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。 From 6551b36fc756b78d11bee66c4b95f0799ce2b3e4 Mon Sep 17 00:00:00 2001 From: AC_Oier Date: Thu, 17 Feb 2022 09:21:29 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E2=9C=A8feat:=20Add=20688?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "Index/\347\272\277\346\200\247 DP.md" | 1 + ...10\344\270\255\347\255\211\357\274\211.md" | 100 ++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 "LeetCode/681-690/688. \351\252\221\345\243\253\345\234\250\346\243\213\347\233\230\344\270\212\347\232\204\346\246\202\347\216\207\357\274\210\344\270\255\347\255\211\357\274\211.md" diff --git "a/Index/\347\272\277\346\200\247 DP.md" "b/Index/\347\272\277\346\200\247 DP.md" index f6c32803..39aec74e 100644 --- "a/Index/\347\272\277\346\200\247 DP.md" +++ "b/Index/\347\272\277\346\200\247 DP.md" @@ -13,6 +13,7 @@ | [639. 解码方法 II](https://leetcode-cn.com/problems/decode-ways-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/decode-ways-ii/solution/gong-shui-san-xie-fen-qing-kuang-tao-lun-902h/) | 困难 | 🤩🤩🤩🤩 | | [650. 只有两个键的键盘](https://leetcode-cn.com/problems/2-keys-keyboard/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/2-keys-keyboard/solution/gong-shui-san-xie-yi-ti-san-jie-dong-tai-f035/) | 中等 | 🤩🤩🤩🤩 | | [678. 有效的括号字符串](https://leetcode-cn.com/problems/valid-parenthesis-string/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/valid-parenthesis-string/solution/gong-shui-san-xie-yi-ti-shuang-jie-dong-801rq/) | 中等 | 🤩🤩🤩🤩🤩 | +| [688. 骑士在棋盘上的概率](https://leetcode-cn.com/problems/knight-probability-in-chessboard/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/knight-probability-in-chessboard/solution/gong-shui-san-xie-jian-dan-qu-jian-dp-yu-st8l/) | 中等 | 🤩🤩🤩🤩🤩 | | [1137. 第 N 个泰波那契数](https://leetcode-cn.com/problems/n-th-tribonacci-number/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/n-th-tribonacci-number/solution/gong-shui-san-xie-yi-ti-si-jie-die-dai-d-m1ie/) | 简单 | 🤩🤩🤩🤩 | | [1220. 统计元音字母序列的数目](https://leetcode-cn.com/problems/count-vowels-permutation/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/count-vowels-permutation/solution/gong-shui-san-xie-yi-ti-shuang-jie-xian-n8f4o/) | 困难 | 🤩🤩🤩🤩 | | [1751. 最多可以参加的会议数目 II](https://leetcode-cn.com/problems/maximum-number-of-events-that-can-be-attended-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximum-number-of-events-that-can-be-attended-ii/solution/po-su-dp-er-fen-dp-jie-fa-by-ac_oier-88du/) | 困难 | 🤩🤩🤩 | diff --git "a/LeetCode/681-690/688. \351\252\221\345\243\253\345\234\250\346\243\213\347\233\230\344\270\212\347\232\204\346\246\202\347\216\207\357\274\210\344\270\255\347\255\211\357\274\211.md" "b/LeetCode/681-690/688. \351\252\221\345\243\253\345\234\250\346\243\213\347\233\230\344\270\212\347\232\204\346\246\202\347\216\207\357\274\210\344\270\255\347\255\211\357\274\211.md" new file mode 100644 index 00000000..cc88f08b --- /dev/null +++ "b/LeetCode/681-690/688. \351\252\221\345\243\253\345\234\250\346\243\213\347\233\230\344\270\212\347\232\204\346\246\202\347\216\207\357\274\210\344\270\255\347\255\211\357\274\211.md" @@ -0,0 +1,100 @@ +### 题目描述 + +这是 LeetCode 上的 **[688. 骑士在棋盘上的概率](https://leetcode-cn.com/problems/knight-probability-in-chessboard/solution/gong-shui-san-xie-jian-dan-qu-jian-dp-yu-st8l/)** ,难度为 **中等**。 + +Tag : 「线性 DP」 + + + +在一个 $n x n$ 的国际象棋棋盘上,一个骑士从单元格 $(row, column)$ 开始,并尝试进行 $k$ 次移动。行和列是 从 $0$ 开始 的,所以左上单元格是 $(0,0)$ ,右下单元格是 $(n - 1, n - 1)$ 。 + +象棋骑士有 $8$ 种可能的走法,如下图所示。每次移动在基本方向上是两个单元格,然后在正交方向上是一个单元格。 + +![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/10/12/knight.png) + +每次骑士要移动时,它都会随机从 $8$ 种可能的移动中选择一种(即使棋子会离开棋盘),然后移动到那里。 + +骑士继续移动,直到它走了 $k$ 步或离开了棋盘。 + +返回 骑士在棋盘停止移动后仍留在棋盘上的概率 。 + +示例 1: +``` +输入: n = 3, k = 2, row = 0, column = 0 + +输出: 0.0625 + +解释: 有两步(到(1,2),(2,1))可以让骑士留在棋盘上。 +在每一个位置上,也有两种移动可以让骑士留在棋盘上。 +骑士留在棋盘上的总概率是0.0625。 +``` +示例 2: +``` +输入: n = 1, k = 0, row = 0, column = 0 + +输出: 1.00000 +``` + +提示: +* $1 <= n <= 25$ +* $0 <= k <= 100$ +* $0 <= row, column <= n$ + +--- + +### 线性 DP + +**定义 $f[i][j][p]$ 为从位置 $(i, j)$ 出发,使用步数不超过 $p$ 步,最后仍在棋盘内的概率。** + +不失一般性考虑 $f[i][j][p]$ 该如何转移,根据题意,移动规则为「八连通」,对下一步的落点 $(nx, ny)$ 进行分情况讨论即可: + +* 由于计算的是仍在棋盘内的概率,因此对于 $(nx, ny)$ 在棋盘外的情况,无须考虑; +* 若下一步的落点 $(nx, ny)$ 在棋盘内,其剩余可用步数为 $p - 1$,则最后仍在棋盘的概率为 $f[nx][ny][p - 1]$,则落点 $(nx, ny)$ 对 $f[i][j][p]$ 的贡献为 $f[nx][ny][p - 1] \times \frac{1}{8}$,其中 $\frac{1}{8}$ 为事件「**从 $(i, j)$ 走到 $(nx, ny)$**」的概率(八连通移动等概率发生),该事件与「**到达 $(nx, ny)$ 后进行后续移动并留在棋盘**」为相互独立事件。 + +最终的 $f[i][j][p]$ 为「八连通」落点的概率之和,即有: + +$$ +f[i][j][p] = \sum {f[nx][ny][p - 1] \times \frac{1}{8}} +$$ + +代码: +```Java +class Solution { + int[][] dirs = new int[][]{{-1,-2},{-1,2},{1,-2},{1,2},{-2,1},{-2,-1},{2,1},{2,-1}}; + public double knightProbability(int n, int k, int row, int column) { + double[][][] f = new double[n][n][k + 1]; + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + f[i][j][0] = 1; + } + } + for (int p = 1; p <= k; p++) { + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + for (int[] d : dirs) { + int nx = i + d[0], ny = j + d[1]; + if (nx < 0 || nx >= n || ny < 0 || ny >= n) continue; + f[i][j][p] += f[nx][ny][p - 1] / 8; + } + } + } + } + return f[row][column][k]; + } +} +``` +* 时间复杂度:令某个位置可联通的格子数量 $C = 8$,复杂度为 $O(n^2 * k * C)$ +* 空间复杂度:$O(n^2 * k)$ + +--- + +### 最后 + +这是我们「刷穿 LeetCode」系列文章的第 `No.688` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。 + +在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。 + +为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。 + +在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。 +