From be3a20af540edb70d8400b66539b48fbf2a73779 Mon Sep 17 00:00:00 2001 From: Michael Xu Date: Sat, 16 Sep 2023 22:14:56 -0400 Subject: [PATCH] feat: add rust solution to lc problem: No.0847 --- .../README.md | 39 +++++++++++++++++++ .../README_EN.md | 39 +++++++++++++++++++ .../Solution.rs | 34 ++++++++++++++++ 3 files changed, 112 insertions(+) create mode 100644 solution/0800-0899/0847.Shortest Path Visiting All Nodes/Solution.rs diff --git a/solution/0800-0899/0847.Shortest Path Visiting All Nodes/README.md b/solution/0800-0899/0847.Shortest Path Visiting All Nodes/README.md index 4167796357652..e684816823713 100644 --- a/solution/0800-0899/0847.Shortest Path Visiting All Nodes/README.md +++ b/solution/0800-0899/0847.Shortest Path Visiting All Nodes/README.md @@ -285,6 +285,45 @@ public: }; ``` +### **Rust** + +```rust +use std::collections::VecDeque; + +impl Solution { + #[allow(dead_code)] + pub fn shortest_path_length(graph: Vec>) -> i32 { + let n = graph.len(); + let mut vis = vec![vec![false; 1 << n]; n]; + let mut q = VecDeque::new(); + + // Initialize the queue + for i in 0..n { + q.push_back(((i, 1 << i), 0)); + vis[i][1 << i] = true; + } + + // Begin BFS + while !q.is_empty() { + let ((i, st), count) = q.pop_front().unwrap(); + if st == ((1 << n) - 1) { + return count; + } + // If the path has not been visited + for j in &graph[i] { + let nst = st | (1 << *j); + if !vis[*j as usize][nst] { + q.push_back(((*j as usize, nst), count + 1)); + vis[*j as usize][nst] = true; + } + } + } + + -1 + } +} +``` + ### **Go** ```go diff --git a/solution/0800-0899/0847.Shortest Path Visiting All Nodes/README_EN.md b/solution/0800-0899/0847.Shortest Path Visiting All Nodes/README_EN.md index 69a813167c2c3..669e5a3deadb2 100644 --- a/solution/0800-0899/0847.Shortest Path Visiting All Nodes/README_EN.md +++ b/solution/0800-0899/0847.Shortest Path Visiting All Nodes/README_EN.md @@ -247,6 +247,45 @@ public: }; ``` +### **Rust** + +```rust +use std::collections::VecDeque; + +impl Solution { + #[allow(dead_code)] + pub fn shortest_path_length(graph: Vec>) -> i32 { + let n = graph.len(); + let mut vis = vec![vec![false; 1 << n]; n]; + let mut q = VecDeque::new(); + + // Initialize the queue + for i in 0..n { + q.push_back(((i, 1 << i), 0)); + vis[i][1 << i] = true; + } + + // Begin BFS + while !q.is_empty() { + let ((i, st), count) = q.pop_front().unwrap(); + if st == ((1 << n) - 1) { + return count; + } + // If the path has not been visited + for j in &graph[i] { + let nst = st | (1 << *j); + if !vis[*j as usize][nst] { + q.push_back(((*j as usize, nst), count + 1)); + vis[*j as usize][nst] = true; + } + } + } + + -1 + } +} +``` + ### **Go** ```go diff --git a/solution/0800-0899/0847.Shortest Path Visiting All Nodes/Solution.rs b/solution/0800-0899/0847.Shortest Path Visiting All Nodes/Solution.rs new file mode 100644 index 0000000000000..cc1396894a2c9 --- /dev/null +++ b/solution/0800-0899/0847.Shortest Path Visiting All Nodes/Solution.rs @@ -0,0 +1,34 @@ +use std::collections::VecDeque; + +impl Solution { + #[allow(dead_code)] + pub fn shortest_path_length(graph: Vec>) -> i32 { + let n = graph.len(); + let mut vis = vec![vec![false; 1 << n]; n]; + let mut q = VecDeque::new(); + + // Initialize the queue + for i in 0..n { + q.push_back(((i, 1 << i), 0)); + vis[i][1 << i] = true; + } + + // Begin BFS + while !q.is_empty() { + let ((i, st), count) = q.pop_front().unwrap(); + if st == ((1 << n) - 1) { + return count; + } + // If the path has not been visited + for j in &graph[i] { + let nst = st | (1 << *j); + if !vis[*j as usize][nst] { + q.push_back(((*j as usize, nst), count + 1)); + vis[*j as usize][nst] = true; + } + } + } + + -1 + } +} \ No newline at end of file