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 825f0de

Browse files
authored
feat: add rust solution to lc problem: No.0847 (#1638)
1 parent 547caf7 commit 825f0de

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

‎solution/0800-0899/0847.Shortest Path Visiting All Nodes/README.md‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,45 @@ public:
285285
};
286286
```
287287

288+
### **Rust**
289+
290+
```rust
291+
use std::collections::VecDeque;
292+
293+
impl Solution {
294+
#[allow(dead_code)]
295+
pub fn shortest_path_length(graph: Vec<Vec<i32>>) -> i32 {
296+
let n = graph.len();
297+
let mut vis = vec![vec![false; 1 << n]; n];
298+
let mut q = VecDeque::new();
299+
300+
// Initialize the queue
301+
for i in 0..n {
302+
q.push_back(((i, 1 << i), 0));
303+
vis[i][1 << i] = true;
304+
}
305+
306+
// Begin BFS
307+
while !q.is_empty() {
308+
let ((i, st), count) = q.pop_front().unwrap();
309+
if st == ((1 << n) - 1) {
310+
return count;
311+
}
312+
// If the path has not been visited
313+
for j in &graph[i] {
314+
let nst = st | (1 << *j);
315+
if !vis[*j as usize][nst] {
316+
q.push_back(((*j as usize, nst), count + 1));
317+
vis[*j as usize][nst] = true;
318+
}
319+
}
320+
}
321+
322+
-1
323+
}
324+
}
325+
```
326+
288327
### **Go**
289328

290329
```go

‎solution/0800-0899/0847.Shortest Path Visiting All Nodes/README_EN.md‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,45 @@ public:
247247
};
248248
```
249249

250+
### **Rust**
251+
252+
```rust
253+
use std::collections::VecDeque;
254+
255+
impl Solution {
256+
#[allow(dead_code)]
257+
pub fn shortest_path_length(graph: Vec<Vec<i32>>) -> i32 {
258+
let n = graph.len();
259+
let mut vis = vec![vec![false; 1 << n]; n];
260+
let mut q = VecDeque::new();
261+
262+
// Initialize the queue
263+
for i in 0..n {
264+
q.push_back(((i, 1 << i), 0));
265+
vis[i][1 << i] = true;
266+
}
267+
268+
// Begin BFS
269+
while !q.is_empty() {
270+
let ((i, st), count) = q.pop_front().unwrap();
271+
if st == ((1 << n) - 1) {
272+
return count;
273+
}
274+
// If the path has not been visited
275+
for j in &graph[i] {
276+
let nst = st | (1 << *j);
277+
if !vis[*j as usize][nst] {
278+
q.push_back(((*j as usize, nst), count + 1));
279+
vis[*j as usize][nst] = true;
280+
}
281+
}
282+
}
283+
284+
-1
285+
}
286+
}
287+
```
288+
250289
### **Go**
251290

252291
```go
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use std::collections::VecDeque;
2+
3+
impl Solution {
4+
#[allow(dead_code)]
5+
pub fn shortest_path_length(graph: Vec<Vec<i32>>) -> i32 {
6+
let n = graph.len();
7+
let mut vis = vec![vec![false; 1 << n]; n];
8+
let mut q = VecDeque::new();
9+
10+
// Initialize the queue
11+
for i in 0..n {
12+
q.push_back(((i, 1 << i), 0));
13+
vis[i][1 << i] = true;
14+
}
15+
16+
// Begin BFS
17+
while !q.is_empty() {
18+
let ((i, st), count) = q.pop_front().unwrap();
19+
if st == ((1 << n) - 1) {
20+
return count;
21+
}
22+
// If the path has not been visited
23+
for j in &graph[i] {
24+
let nst = st | (1 << *j);
25+
if !vis[*j as usize][nst] {
26+
q.push_back(((*j as usize, nst), count + 1));
27+
vis[*j as usize][nst] = true;
28+
}
29+
}
30+
}
31+
32+
-1
33+
}
34+
}

0 commit comments

Comments
(0)

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