Skip to content

Commit d3634e3

Browse files
authored
feat: add rust solution to lc problem: No.0688 (doocs#1268)
1 parent f0dbaa2 commit d3634e3

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed

solution/0600-0699/0688.Knight Probability in Chessboard/README.md

+45
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,51 @@ public:
154154
};
155155
```
156156
157+
### **Rust**
158+
159+
```rust
160+
const DIR: [(i32, i32); 8] = [(-2, -1), (2, -1), (-1, -2), (1, -2), (2, 1), (-2, 1), (1, 2), (-1, 2)];
161+
const P: f64 = 1.0 / 8.0;
162+
163+
impl Solution {
164+
#[allow(dead_code)]
165+
pub fn knight_probability(n: i32, k: i32, row: i32, column: i32) -> f64 {
166+
// Here dp[i][j][k] represents through `i` steps, the probability that the knight stays on the board
167+
// Starts from row: `j`, column: `k`
168+
let mut dp: Vec<Vec<Vec<f64>>> = vec![vec![vec![0 as f64; n as usize]; n as usize]; k as usize + 1];
169+
170+
// Initialize the dp vector, since dp[0][j][k] should be 1
171+
for j in 0..n as usize {
172+
for k in 0..n as usize {
173+
dp[0][j][k] = 1.0;
174+
}
175+
}
176+
177+
// Begin the actual dp process
178+
for i in 1..=k {
179+
for j in 0..n {
180+
for k in 0..n {
181+
for (dx, dy) in DIR {
182+
let x = j + dx;
183+
let y = k + dy;
184+
if Self::check_bounds(x, y, n, n) {
185+
dp[i as usize][j as usize][k as usize] += P * dp[i as usize - 1][x as usize][y as usize];
186+
}
187+
}
188+
}
189+
}
190+
}
191+
192+
dp[k as usize][row as usize][column as usize]
193+
}
194+
195+
#[allow(dead_code)]
196+
fn check_bounds(i: i32, j: i32, n: i32, m: i32) -> bool {
197+
i >= 0 && i < n && j >= 0 && j < m
198+
}
199+
}
200+
```
201+
157202
### **Go**
158203

159204
```go

solution/0600-0699/0688.Knight Probability in Chessboard/README_EN.md

+45
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,51 @@ public:
142142
};
143143
```
144144
145+
### **Rust**
146+
147+
```rust
148+
const DIR: [(i32, i32); 8] = [(-2, -1), (2, -1), (-1, -2), (1, -2), (2, 1), (-2, 1), (1, 2), (-1, 2)];
149+
const P: f64 = 1.0 / 8.0;
150+
151+
impl Solution {
152+
#[allow(dead_code)]
153+
pub fn knight_probability(n: i32, k: i32, row: i32, column: i32) -> f64 {
154+
// Here dp[i][j][k] represents through `i` steps, the probability that the knight stays on the board
155+
// Starts from row: `j`, column: `k`
156+
let mut dp: Vec<Vec<Vec<f64>>> = vec![vec![vec![0 as f64; n as usize]; n as usize]; k as usize + 1];
157+
158+
// Initialize the dp vector, since dp[0][j][k] should be 1
159+
for j in 0..n as usize {
160+
for k in 0..n as usize {
161+
dp[0][j][k] = 1.0;
162+
}
163+
}
164+
165+
// Begin the actual dp process
166+
for i in 1..=k {
167+
for j in 0..n {
168+
for k in 0..n {
169+
for (dx, dy) in DIR {
170+
let x = j + dx;
171+
let y = k + dy;
172+
if Self::check_bounds(x, y, n, n) {
173+
dp[i as usize][j as usize][k as usize] += P * dp[i as usize - 1][x as usize][y as usize];
174+
}
175+
}
176+
}
177+
}
178+
}
179+
180+
dp[k as usize][row as usize][column as usize]
181+
}
182+
183+
#[allow(dead_code)]
184+
fn check_bounds(i: i32, j: i32, n: i32, m: i32) -> bool {
185+
i >= 0 && i < n && j >= 0 && j < m
186+
}
187+
}
188+
```
189+
145190
### **Go**
146191

147192
```go
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const DIR: [(i32, i32); 8] = [(-2, -1), (2, -1), (-1, -2), (1, -2), (2, 1), (-2, 1), (1, 2), (-1, 2)];
2+
const P: f64 = 1.0 / 8.0;
3+
4+
impl Solution {
5+
#[allow(dead_code)]
6+
pub fn knight_probability(n: i32, k: i32, row: i32, column: i32) -> f64 {
7+
// Here dp[i][j][k] represents through `i` steps, the probability that the knight stays on the board
8+
// Starts from row: `j`, column: `k`
9+
let mut dp: Vec<Vec<Vec<f64>>> = vec![vec![vec![0 as f64; n as usize]; n as usize]; k as usize + 1];
10+
11+
// Initialize the dp vector, since dp[0][j][k] should be 1
12+
for j in 0..n as usize {
13+
for k in 0..n as usize {
14+
dp[0][j][k] = 1.0;
15+
}
16+
}
17+
18+
// Begin the actual dp process
19+
for i in 1..=k {
20+
for j in 0..n {
21+
for k in 0..n {
22+
for (dx, dy) in DIR {
23+
let x = j + dx;
24+
let y = k + dy;
25+
if Self::check_bounds(x, y, n, n) {
26+
dp[i as usize][j as usize][k as usize] += P * dp[i as usize - 1][x as usize][y as usize];
27+
}
28+
}
29+
}
30+
}
31+
}
32+
33+
dp[k as usize][row as usize][column as usize]
34+
}
35+
36+
#[allow(dead_code)]
37+
fn check_bounds(i: i32, j: i32, n: i32, m: i32) -> bool {
38+
i >= 0 && i < n && j >= 0 && j < m
39+
}
40+
}

0 commit comments

Comments
 (0)