Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c76ef3e

Browse files
Merge pull request SharingSource#369 from SharingSource/ac_oier
✨batch: Add 688 & Modify 877
2 parents ae1a589 + 6551b36 commit c76ef3e

File tree

4 files changed

+103
-2
lines changed

4 files changed

+103
-2
lines changed

‎Index/区间 DP.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
| [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/) | 困难 | 🤩🤩🤩 |
44
| [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/) | 中等 | 🤩🤩🤩🤩🤩 |
55
| [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/) | 困难 | 🤩🤩🤩 |
6-
| [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/) | 中等 | 🤩🤩🤩🤩🤩 |
6+
| [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/) | 困难 | 🤩🤩🤩🤩🤩 |
77
| [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/) | 中等 | 🤩🤩🤩🤩 |
88

‎Index/线性 DP.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
| [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/) | 困难 | 🤩🤩🤩🤩 |
1414
| [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/) | 中等 | 🤩🤩🤩🤩 |
1515
| [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/) | 中等 | 🤩🤩🤩🤩🤩 |
16+
| [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/) | 中等 | 🤩🤩🤩🤩🤩 |
1617
| [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/) | 简单 | 🤩🤩🤩🤩 |
1718
| [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/) | 困难 | 🤩🤩🤩🤩 |
1819
| [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/) | 困难 | 🤩🤩🤩 |
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[688. 骑士在棋盘上的概率](https://leetcode-cn.com/problems/knight-probability-in-chessboard/solution/gong-shui-san-xie-jian-dan-qu-jian-dp-yu-st8l/)** ,难度为 **中等**
4+
5+
Tag : 「线性 DP」
6+
7+
8+
9+
在一个 $n x n$ 的国际象棋棋盘上,一个骑士从单元格 $(row, column)$ 开始,并尝试进行 $k$ 次移动。行和列是 从 0ドル$ 开始 的,所以左上单元格是 $(0,0)$ ,右下单元格是 $(n - 1, n - 1)$ 。
10+
11+
象棋骑士有 8ドル$ 种可能的走法,如下图所示。每次移动在基本方向上是两个单元格,然后在正交方向上是一个单元格。
12+
13+
![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/10/12/knight.png)
14+
15+
每次骑士要移动时,它都会随机从 8ドル$ 种可能的移动中选择一种(即使棋子会离开棋盘),然后移动到那里。
16+
17+
骑士继续移动,直到它走了 $k$ 步或离开了棋盘。
18+
19+
返回 骑士在棋盘停止移动后仍留在棋盘上的概率 。
20+
21+
示例 1:
22+
```
23+
输入: n = 3, k = 2, row = 0, column = 0
24+
25+
输出: 0.0625
26+
27+
解释: 有两步(到(1,2),(2,1))可以让骑士留在棋盘上。
28+
在每一个位置上,也有两种移动可以让骑士留在棋盘上。
29+
骑士留在棋盘上的总概率是0.0625。
30+
```
31+
示例 2:
32+
```
33+
输入: n = 1, k = 0, row = 0, column = 0
34+
35+
输出: 1.00000
36+
```
37+
38+
提示:
39+
* 1ドル <= n <= 25$
40+
* 0ドル <= k <= 100$
41+
* 0ドル <= row, column <= n$
42+
43+
---
44+
45+
### 线性 DP
46+
47+
**定义 $f[i][j][p]$ 为从位置 $(i, j)$ 出发,使用步数不超过 $p$ 步,最后仍在棋盘内的概率。**
48+
49+
不失一般性考虑 $f[i][j][p]$ 该如何转移,根据题意,移动规则为「八连通」,对下一步的落点 $(nx, ny)$ 进行分情况讨论即可:
50+
51+
* 由于计算的是仍在棋盘内的概率,因此对于 $(nx, ny)$ 在棋盘外的情况,无须考虑;
52+
* 若下一步的落点 $(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)$ 后进行后续移动并留在棋盘**」为相互独立事件。
53+
54+
最终的 $f[i][j][p]$ 为「八连通」落点的概率之和,即有:
55+
56+
$$
57+
f[i][j][p] = \sum {f[nx][ny][p - 1] \times \frac{1}{8}}
58+
$$
59+
60+
代码:
61+
```Java
62+
class Solution {
63+
int[][] dirs = new int[][]{{-1,-2},{-1,2},{1,-2},{1,2},{-2,1},{-2,-1},{2,1},{2,-1}};
64+
public double knightProbability(int n, int k, int row, int column) {
65+
double[][][] f = new double[n][n][k + 1];
66+
for (int i = 0; i < n; i++) {
67+
for (int j = 0; j < n; j++) {
68+
f[i][j][0] = 1;
69+
}
70+
}
71+
for (int p = 1; p <= k; p++) {
72+
for (int i = 0; i < n; i++) {
73+
for (int j = 0; j < n; j++) {
74+
for (int[] d : dirs) {
75+
int nx = i + d[0], ny = j + d[1];
76+
if (nx < 0 || nx >= n || ny < 0 || ny >= n) continue;
77+
f[i][j][p] += f[nx][ny][p - 1] / 8;
78+
}
79+
}
80+
}
81+
}
82+
return f[row][column][k];
83+
}
84+
}
85+
```
86+
* 时间复杂度:令某个位置可联通的格子数量 $C = 8,ドル复杂度为 $O(n^2 * k * C)$
87+
* 空间复杂度:$O(n^2 * k)$
88+
89+
---
90+
91+
### 最后
92+
93+
这是我们「刷穿 LeetCode」系列文章的第 `No.688` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
94+
95+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
96+
97+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
98+
99+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
100+

‎LeetCode/871-880/877. 石子游戏(中等).md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ class Solution {
120120

121121
### 最后
122122

123-
这是我们「刷穿 LeetCode」系列文章的第 `No.877` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先将所有不带锁的题目刷完
123+
这是我们「刷穿 LeetCode」系列文章的第 `No.877` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完
124124

125125
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
126126

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /