File tree 3 files changed +112
-0
lines changed
solution/0800-0899/0847.Shortest Path Visiting All Nodes
3 files changed +112
-0
lines changed Original file line number Diff line number Diff line change @@ -285,6 +285,45 @@ public:
285
285
};
286
286
```
287
287
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
+
288
327
### ** Go**
289
328
290
329
``` go
Original file line number Diff line number Diff line change @@ -247,6 +247,45 @@ public:
247
247
};
248
248
```
249
249
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
+
250
289
### ** Go**
251
290
252
291
``` go
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments