Skip to content

Commit 1a3032c

Browse files
committed
feat: add solutions to lc problem: No.0909
No.0909.Snakes and Ladders
1 parent bb44b78 commit 1a3032c

File tree

6 files changed

+460
-2
lines changed

6 files changed

+460
-2
lines changed

solution/0900-0999/0909.Snakes and Ladders/README.md

+157-1
Original file line numberDiff line numberDiff line change
@@ -71,22 +71,178 @@
7171

7272
<!-- 这里可写通用的实现逻辑 -->
7373

74+
**方法一:BFS**
75+
7476
<!-- tabs:start -->
7577

7678
### **Python3**
7779

7880
<!-- 这里可写当前语言的特殊实现逻辑 -->
7981

8082
```python
81-
83+
class Solution:
84+
def snakesAndLadders(self, board: List[List[int]]) -> int:
85+
def get(x):
86+
i, j = (x - 1) // n, (x - 1) % n
87+
if i & 1:
88+
j = n - 1 - j
89+
return n - 1 - i, j
90+
91+
n = len(board)
92+
q = deque([1])
93+
vis = {1}
94+
ans = 0
95+
while q:
96+
for _ in range(len(q)):
97+
curr = q.popleft()
98+
if curr == n * n:
99+
return ans
100+
for next in range(curr + 1, min(curr + 7, n * n + 1)):
101+
i, j = get(next)
102+
if board[i][j] != -1:
103+
next = board[i][j]
104+
if next not in vis:
105+
q.append(next)
106+
vis.add(next)
107+
ans += 1
108+
return -1
82109
```
83110

84111
### **Java**
85112

86113
<!-- 这里可写当前语言的特殊实现逻辑 -->
87114

88115
```java
116+
class Solution {
117+
private int n;
118+
119+
public int snakesAndLadders(int[][] board) {
120+
n = board.length;
121+
Deque<Integer> q = new ArrayDeque<>();
122+
q.offer(1);
123+
boolean[] vis = new boolean[n * n + 1];
124+
vis[1] = true;
125+
int ans = 0;
126+
while (!q.isEmpty()) {
127+
for (int t = q.size(); t > 0; --t) {
128+
int curr = q.poll();
129+
if (curr == n * n) {
130+
return ans;
131+
}
132+
for (int k = curr + 1; k <= Math.min(curr + 6, n * n); ++k) {
133+
int[] p = get(k);
134+
int next = k;
135+
int i = p[0], j = p[1];
136+
if (board[i][j] != -1) {
137+
next = board[i][j];
138+
}
139+
if (!vis[next]) {
140+
vis[next] = true;
141+
q.offer(next);
142+
}
143+
}
144+
}
145+
++ans;
146+
}
147+
return -1;
148+
}
149+
150+
private int[] get(int x) {
151+
int i = (x - 1) / n, j = (x - 1) % n;
152+
if (i % 2 == 1) {
153+
j = n - 1 - j;
154+
}
155+
return new int[]{n - 1 - i, j};
156+
}
157+
}
158+
```
159+
160+
### **C++**
161+
162+
```cpp
163+
class Solution {
164+
public:
165+
int n;
166+
167+
int snakesAndLadders(vector<vector<int>>& board) {
168+
n = board.size();
169+
queue<int> q{{1}};
170+
vector<bool> vis(n * n + 1);
171+
vis[1] = true;
172+
int ans = 0;
173+
while (!q.empty())
174+
{
175+
for (int t = q.size(); t; --t)
176+
{
177+
int curr = q.front();
178+
if (curr == n * n) return ans;
179+
q.pop();
180+
for (int k = curr + 1; k <= min(curr + 6, n * n); ++k)
181+
{
182+
auto p = get(k);
183+
int next = k;
184+
int i = p[0], j = p[1];
185+
if (board[i][j] != -1) next = board[i][j];
186+
if (!vis[next])
187+
{
188+
vis[next] = true;
189+
q.push(next);
190+
}
191+
}
192+
}
193+
++ans;
194+
}
195+
return -1;
196+
}
197+
198+
vector<int> get(int x) {
199+
int i = (x - 1) / n, j = (x - 1) % n;
200+
if (i % 2 == 1) j = n - 1 - j;
201+
return {n - 1 - i, j};
202+
}
203+
};
204+
```
89205
206+
### **Go**
207+
208+
```go
209+
func snakesAndLadders(board [][]int) int {
210+
n := len(board)
211+
get := func(x int) []int {
212+
i, j := (x-1)/n, (x-1)%n
213+
if i%2 == 1 {
214+
j = n - 1 - j
215+
}
216+
return []int{n - 1 - i, j}
217+
}
218+
q := []int{1}
219+
vis := make([]bool, n*n+1)
220+
vis[1] = true
221+
ans := 0
222+
for len(q) > 0 {
223+
for t := len(q); t > 0; t-- {
224+
curr := q[0]
225+
if curr == n*n {
226+
return ans
227+
}
228+
q = q[1:]
229+
for k := curr + 1; k <= curr+6 && k <= n*n; k++ {
230+
p := get(k)
231+
next := k
232+
i, j := p[0], p[1]
233+
if board[i][j] != -1 {
234+
next = board[i][j]
235+
}
236+
if !vis[next] {
237+
vis[next] = true
238+
q = append(q, next)
239+
}
240+
}
241+
}
242+
ans++
243+
}
244+
return -1
245+
}
90246
```
91247

92248
### **...**

solution/0900-0999/0909.Snakes and Ladders/README_EN.md

+157-1
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,174 @@ This is the lowest possible number of moves to reach the last square, so return
6363

6464
## Solutions
6565

66+
BFS.
67+
6668
<!-- tabs:start -->
6769

6870
### **Python3**
6971

7072
```python
71-
73+
class Solution:
74+
def snakesAndLadders(self, board: List[List[int]]) -> int:
75+
def get(x):
76+
i, j = (x - 1) // n, (x - 1) % n
77+
if i & 1:
78+
j = n - 1 - j
79+
return n - 1 - i, j
80+
81+
n = len(board)
82+
q = deque([1])
83+
vis = {1}
84+
ans = 0
85+
while q:
86+
for _ in range(len(q)):
87+
curr = q.popleft()
88+
if curr == n * n:
89+
return ans
90+
for next in range(curr + 1, min(curr + 7, n * n + 1)):
91+
i, j = get(next)
92+
if board[i][j] != -1:
93+
next = board[i][j]
94+
if next not in vis:
95+
q.append(next)
96+
vis.add(next)
97+
ans += 1
98+
return -1
7299
```
73100

74101
### **Java**
75102

76103
```java
104+
class Solution {
105+
private int n;
106+
107+
public int snakesAndLadders(int[][] board) {
108+
n = board.length;
109+
Deque<Integer> q = new ArrayDeque<>();
110+
q.offer(1);
111+
boolean[] vis = new boolean[n * n + 1];
112+
vis[1] = true;
113+
int ans = 0;
114+
while (!q.isEmpty()) {
115+
for (int t = q.size(); t > 0; --t) {
116+
int curr = q.poll();
117+
if (curr == n * n) {
118+
return ans;
119+
}
120+
for (int k = curr + 1; k <= Math.min(curr + 6, n * n); ++k) {
121+
int[] p = get(k);
122+
int next = k;
123+
int i = p[0], j = p[1];
124+
if (board[i][j] != -1) {
125+
next = board[i][j];
126+
}
127+
if (!vis[next]) {
128+
vis[next] = true;
129+
q.offer(next);
130+
}
131+
}
132+
}
133+
++ans;
134+
}
135+
return -1;
136+
}
137+
138+
private int[] get(int x) {
139+
int i = (x - 1) / n, j = (x - 1) % n;
140+
if (i % 2 == 1) {
141+
j = n - 1 - j;
142+
}
143+
return new int[]{n - 1 - i, j};
144+
}
145+
}
146+
```
147+
148+
### **C++**
149+
150+
```cpp
151+
class Solution {
152+
public:
153+
int n;
154+
155+
int snakesAndLadders(vector<vector<int>>& board) {
156+
n = board.size();
157+
queue<int> q{{1}};
158+
vector<bool> vis(n * n + 1);
159+
vis[1] = true;
160+
int ans = 0;
161+
while (!q.empty())
162+
{
163+
for (int t = q.size(); t; --t)
164+
{
165+
int curr = q.front();
166+
if (curr == n * n) return ans;
167+
q.pop();
168+
for (int k = curr + 1; k <= min(curr + 6, n * n); ++k)
169+
{
170+
auto p = get(k);
171+
int next = k;
172+
int i = p[0], j = p[1];
173+
if (board[i][j] != -1) next = board[i][j];
174+
if (!vis[next])
175+
{
176+
vis[next] = true;
177+
q.push(next);
178+
}
179+
}
180+
}
181+
++ans;
182+
}
183+
return -1;
184+
}
185+
186+
vector<int> get(int x) {
187+
int i = (x - 1) / n, j = (x - 1) % n;
188+
if (i % 2 == 1) j = n - 1 - j;
189+
return {n - 1 - i, j};
190+
}
191+
};
192+
```
77193
194+
### **Go**
195+
196+
```go
197+
func snakesAndLadders(board [][]int) int {
198+
n := len(board)
199+
get := func(x int) []int {
200+
i, j := (x-1)/n, (x-1)%n
201+
if i%2 == 1 {
202+
j = n - 1 - j
203+
}
204+
return []int{n - 1 - i, j}
205+
}
206+
q := []int{1}
207+
vis := make([]bool, n*n+1)
208+
vis[1] = true
209+
ans := 0
210+
for len(q) > 0 {
211+
for t := len(q); t > 0; t-- {
212+
curr := q[0]
213+
if curr == n*n {
214+
return ans
215+
}
216+
q = q[1:]
217+
for k := curr + 1; k <= curr+6 && k <= n*n; k++ {
218+
p := get(k)
219+
next := k
220+
i, j := p[0], p[1]
221+
if board[i][j] != -1 {
222+
next = board[i][j]
223+
}
224+
if !vis[next] {
225+
vis[next] = true
226+
q = append(q, next)
227+
}
228+
}
229+
}
230+
ans++
231+
}
232+
return -1
233+
}
78234
```
79235

80236
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class Solution {
2+
public:
3+
int n;
4+
5+
int snakesAndLadders(vector<vector<int>>& board) {
6+
n = board.size();
7+
queue<int> q{{1}};
8+
vector<bool> vis(n * n + 1);
9+
vis[1] = true;
10+
int ans = 0;
11+
while (!q.empty())
12+
{
13+
for (int t = q.size(); t; --t)
14+
{
15+
int curr = q.front();
16+
if (curr == n * n) return ans;
17+
q.pop();
18+
for (int k = curr + 1; k <= min(curr + 6, n * n); ++k)
19+
{
20+
auto p = get(k);
21+
int next = k;
22+
int i = p[0], j = p[1];
23+
if (board[i][j] != -1) next = board[i][j];
24+
if (!vis[next])
25+
{
26+
vis[next] = true;
27+
q.push(next);
28+
}
29+
}
30+
}
31+
++ans;
32+
}
33+
return -1;
34+
}
35+
36+
vector<int> get(int x) {
37+
int i = (x - 1) / n, j = (x - 1) % n;
38+
if (i % 2 == 1) j = n - 1 - j;
39+
return {n - 1 - i, j};
40+
}
41+
};

0 commit comments

Comments
 (0)