File tree Expand file tree Collapse file tree 3 files changed +133
-0
lines changed
solution/0900-0999/0909.Snakes and Ladders Expand file tree Collapse file tree 3 files changed +133
-0
lines changed Original file line number Diff line number Diff line change @@ -279,6 +279,52 @@ function snakesAndLadders(board: number[][]): number {
279279}
280280```
281281
282+ #### Rust
283+
284+ ``` rust
285+ use std :: collections :: {HashSet , VecDeque };
286+
287+ impl Solution {
288+ pub fn snakes_and_ladders (board : Vec <Vec <i32 >>) -> i32 {
289+ let n = board . len ();
290+ let m = (n * n ) as i32 ;
291+ let mut q = VecDeque :: new ();
292+ q . push_back (1 );
293+ let mut vis = HashSet :: new ();
294+ vis . insert (1 );
295+ let mut ans = 0 ;
296+
297+ while ! q . is_empty () {
298+ for _ in 0 .. q . len () {
299+ let x = q . pop_front (). unwrap ();
300+ if x == m {
301+ return ans ;
302+ }
303+ for y in x + 1 ..= i32 :: min (x + 6 , m ) {
304+ let (mut i , mut j ) = ((y - 1 ) / n as i32 , (y - 1 ) % n as i32 );
305+ if i % 2 == 1 {
306+ j = (n as i32 - 1 ) - j ;
307+ }
308+ i = (n as i32 - 1 ) - i ;
309+ let z = if board [i as usize ][j as usize ] == - 1 {
310+ y
311+ } else {
312+ board [i as usize ][j as usize ]
313+ };
314+ if ! vis . contains (& z ) {
315+ vis . insert (z );
316+ q . push_back (z );
317+ }
318+ }
319+ }
320+ ans += 1 ;
321+ }
322+
323+ - 1
324+ }
325+ }
326+ ```
327+
282328<!-- tabs:end -->
283329
284330<!-- solution:end -->
Original file line number Diff line number Diff line change @@ -277,6 +277,52 @@ function snakesAndLadders(board: number[][]): number {
277277}
278278```
279279
280+ #### Rust
281+
282+ ``` rust
283+ use std :: collections :: {HashSet , VecDeque };
284+
285+ impl Solution {
286+ pub fn snakes_and_ladders (board : Vec <Vec <i32 >>) -> i32 {
287+ let n = board . len ();
288+ let m = (n * n ) as i32 ;
289+ let mut q = VecDeque :: new ();
290+ q . push_back (1 );
291+ let mut vis = HashSet :: new ();
292+ vis . insert (1 );
293+ let mut ans = 0 ;
294+
295+ while ! q . is_empty () {
296+ for _ in 0 .. q . len () {
297+ let x = q . pop_front (). unwrap ();
298+ if x == m {
299+ return ans ;
300+ }
301+ for y in x + 1 ..= i32 :: min (x + 6 , m ) {
302+ let (mut i , mut j ) = ((y - 1 ) / n as i32 , (y - 1 ) % n as i32 );
303+ if i % 2 == 1 {
304+ j = (n as i32 - 1 ) - j ;
305+ }
306+ i = (n as i32 - 1 ) - i ;
307+ let z = if board [i as usize ][j as usize ] == - 1 {
308+ y
309+ } else {
310+ board [i as usize ][j as usize ]
311+ };
312+ if ! vis . contains (& z ) {
313+ vis . insert (z );
314+ q . push_back (z );
315+ }
316+ }
317+ }
318+ ans += 1 ;
319+ }
320+
321+ - 1
322+ }
323+ }
324+ ```
325+
280326<!-- tabs:end -->
281327
282328<!-- solution:end -->
Original file line number Diff line number Diff line change 1+ use std:: collections:: { HashSet , VecDeque } ;
2+
3+ impl Solution {
4+ pub fn snakes_and_ladders ( board : Vec < Vec < i32 > > ) -> i32 {
5+ let n = board. len ( ) ;
6+ let m = ( n * n) as i32 ;
7+ let mut q = VecDeque :: new ( ) ;
8+ q. push_back ( 1 ) ;
9+ let mut vis = HashSet :: new ( ) ;
10+ vis. insert ( 1 ) ;
11+ let mut ans = 0 ;
12+
13+ while !q. is_empty ( ) {
14+ for _ in 0 ..q. len ( ) {
15+ let x = q. pop_front ( ) . unwrap ( ) ;
16+ if x == m {
17+ return ans;
18+ }
19+ for y in x + 1 ..=i32:: min ( x + 6 , m) {
20+ let ( mut i, mut j) = ( ( y - 1 ) / n as i32 , ( y - 1 ) % n as i32 ) ;
21+ if i % 2 == 1 {
22+ j = ( n as i32 - 1 ) - j;
23+ }
24+ i = ( n as i32 - 1 ) - i;
25+ let z = if board[ i as usize ] [ j as usize ] == -1 {
26+ y
27+ } else {
28+ board[ i as usize ] [ j as usize ]
29+ } ;
30+ if !vis. contains ( & z) {
31+ vis. insert ( z) ;
32+ q. push_back ( z) ;
33+ }
34+ }
35+ }
36+ ans += 1 ;
37+ }
38+
39+ -1
40+ }
41+ }
You can’t perform that action at this time.
0 commit comments