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 c1f8da4

Browse files
✨feat: Add 909
1 parent d1fb6ca commit c1f8da4

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

‎Index/图论 BFS.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
| [403. 青蛙过河](https://leetcode-cn.com/problems/frog-jump/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/frog-jump/solution/gong-shui-san-xie-yi-ti-duo-jie-jiang-di-74fw/) | 困难 | 🤩🤩🤩🤩🤩 |
55
| [752. 打开转盘锁](https://leetcode-cn.com/problems/open-the-lock/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/open-the-lock/solution/gong-shui-san-xie-yi-ti-shuang-jie-shuan-wyr9/) | 中等 | 🤩🤩🤩🤩 |
66
| [773. 滑动谜题](https://leetcode-cn.com/problems/sliding-puzzle/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/sliding-puzzle/solution/gong-shui-san-xie-fa-hui-a-suan-fa-zui-d-3go8/) | 困难 | 🤩🤩🤩🤩 |
7+
| [909. 蛇梯棋](https://leetcode-cn.com/problems/snakes-and-ladders/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/snakes-and-ladders/solution/gong-shui-san-xie-bfs-mo-ni-by-ac_oier-woh6/) | 中等 | 🤩🤩🤩🤩 |
78

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[909. 蛇梯棋]()** ,难度为 **中等**
4+
5+
Tag : 「图论 BFS」
6+
7+
8+
9+
$N * N$ 的棋盘 board 上,按从 1 到 $N*N$ 的数字给方格编号,编号 从左下角开始,每一行交替方向。
10+
11+
例如,一块 6ドル x 6$ 大小的棋盘,编号如下:
12+
13+
r 行 c 列的棋盘,按前述方法编号,棋盘格中可能存在 "蛇" 或 "梯子";如果 `board[r][c] != -1`,那个蛇或梯子的目的地将会是 `board[r][c]`
14+
15+
玩家从棋盘上的方格 1 (总是在最后一行、第一列)开始出发。
16+
17+
每一回合,玩家需要从当前方格 x 开始出发,按下述要求前进:
18+
19+
* 选定目标方格:选择从编号 `x+1`,`x+2`,`x+3`,`x+4`,`x+5`,或者 `x+6` 的方格中选出一个目标方格 s ,目标方格的编号 <= $N*N$。
20+
* 该选择模拟了掷骰子的情景,无论棋盘大小如何,你的目的地范围也只能处于区间 [x+1, x+6] 之间。
21+
* 传送玩家:如果目标方格 S 处存在蛇或梯子,那么玩家会传送到蛇或梯子的目的地。否则,玩家传送到目标方格 S。
22+
注意,玩家在每回合的前进过程中最多只能爬过蛇或梯子一次:就算目的地是另一条蛇或梯子的起点,你也不会继续移动。
23+
24+
返回达到方格 $N*N$ 所需的最少移动次数,如果不可能,则返回 -1。
25+
26+
示例:
27+
```
28+
输入:[
29+
[-1,-1,-1,-1,-1,-1],
30+
[-1,-1,-1,-1,-1,-1],
31+
[-1,-1,-1,-1,-1,-1],
32+
[-1,35,-1,-1,13,-1],
33+
[-1,-1,-1,-1,-1,-1],
34+
[-1,15,-1,-1,-1,-1]]
35+
36+
输出:4
37+
38+
解释:
39+
首先,从方格 1 [第 5 行,第 0 列] 开始。
40+
你决定移动到方格 2,并必须爬过梯子移动到到方格 15。
41+
然后你决定移动到方格 17 [第 3 行,第 5 列],必须爬过蛇到方格 13。
42+
然后你决定移动到方格 14,且必须通过梯子移动到方格 35。
43+
然后你决定移动到方格 36, 游戏结束。
44+
可以证明你需要至少 4 次移动才能到达第 N*N 个方格,所以答案是 4。
45+
```
46+
47+
提示:
48+
* 2 <= board.length = board[0].length <= 20
49+
* `board[i][j]` 介于 1 和 $N*N$ 之间或者等于 -1。
50+
* 编号为 1 的方格上没有蛇或梯子。
51+
* 编号为 $N*N$ 的方格上没有蛇或梯子。
52+
53+
---
54+
55+
### BFS
56+
57+
最多有 20ドル * 20$ 个格子,直接使用常规的单向 `BFS` 进行求解即可。
58+
59+
为了方便我们可以按照题目给定的意思,将二维的矩阵「扁平化」为一维的矩阵,然后再按照规则进行 `BFS`
60+
61+
代码:
62+
```Java []
63+
class Solution {
64+
int n;
65+
int[] nums;
66+
public int snakesAndLadders(int[][] board) {
67+
n = board.length;
68+
if (board[0][0] != -1) return -1;
69+
nums = new int[n * n + 1];
70+
boolean isRight = true;
71+
for (int i = n - 1, idx = 1; i >= 0; i--) {
72+
for (int j = (isRight ? 0 : n - 1); isRight ? j < n : j >= 0; j += isRight ? 1 : -1) {
73+
nums[idx++] = board[i][j];
74+
}
75+
isRight = !isRight;
76+
}
77+
int ans = bfs();
78+
return ans;
79+
}
80+
int bfs() {
81+
Deque<Integer> d = new ArrayDeque<>();
82+
Map<Integer, Integer> m = new HashMap<>();
83+
d.addLast(1);
84+
m.put(1, 0);
85+
while (!d.isEmpty()) {
86+
int poll = d.pollFirst();
87+
int step = m.get(poll);
88+
if (poll == n * n) return step;
89+
for (int i = 1; i <= 6; i++) {
90+
int np = poll + i;
91+
if (np <= 0 || np > n * n) continue;
92+
if (nums[np] != -1) np = nums[np];
93+
if (m.containsKey(np)) continue;
94+
m.put(np, step + 1);
95+
d.addLast(np);
96+
}
97+
}
98+
return -1;
99+
}
100+
}
101+
```
102+
* 时间复杂度:$O(n^2)$
103+
* 空间复杂度:$O(n^2)$
104+
105+
---
106+
107+
### 最后
108+
109+
这是我们「刷穿 LeetCode」系列文章的第 `No.909` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先将所有不带锁的题目刷完。
110+
111+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
112+
113+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
114+
115+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
116+

0 commit comments

Comments
(0)

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