1
- use std:: collections:: { VecDeque , HashSet } ;
1
+ use std:: collections:: { HashSet , VecDeque } ;
2
2
3
3
pub enum Direction {
4
4
North ,
5
5
East ,
6
6
South ,
7
- West
7
+ West ,
8
8
}
9
9
10
- const DIRECTIONS : [ Direction ; 4 ] = [
11
- Direction :: North ,
10
+ const DIRECTIONS : [ Direction ; 4 ] = [
11
+ Direction :: North ,
12
12
Direction :: East ,
13
13
Direction :: South ,
14
- Direction :: West
14
+ Direction :: West ,
15
15
] ;
16
16
17
17
#[ derive( Debug , Hash , PartialEq , Eq , Clone , Copy ) ]
18
- pub struct Point {
18
+ pub struct Point {
19
19
row : usize ,
20
- col : usize
20
+ col : usize ,
21
21
}
22
22
impl Point {
23
- pub fn new ( row : usize , col : usize ) -> Self {
24
- Point { row, col}
23
+ pub fn new ( row : usize , col : usize ) -> Self {
24
+ Point { row, col }
25
25
}
26
26
pub fn to_uv ( & self ) -> ( isize , isize ) {
27
27
let ( row, col) = ( self . row as isize , self . col as isize ) ;
28
28
( row + col, row - col)
29
29
}
30
30
}
31
31
32
- pub struct Map {
32
+ pub struct Map {
33
33
width : usize ,
34
34
height : usize ,
35
35
start : Point ,
36
36
end : Point ,
37
- pub matrix : Vec < Vec < Option < usize > > >
37
+ pub matrix : Vec < Vec < Option < usize > > > ,
38
38
}
39
39
40
40
impl Map {
41
- fn get ( & self , p : & Point ) -> Option < usize > {
41
+ fn get ( & self , p : & Point ) -> Option < usize > {
42
42
self . matrix [ p. row ] [ p. col ]
43
43
}
44
44
45
45
pub fn neighbors ( & self , p : & Point ) -> impl Iterator < Item = Point > + ' _ {
46
46
let & Point { row, col } = p;
47
- DIRECTIONS . iter ( ) . filter_map ( move |d| {
48
-
49
- match d {
50
- Direction :: North if row > 0 => Some ( Point :: new ( row - 1 , col) ) ,
51
- Direction :: West if col > 0 => Some ( Point :: new ( row, col - 1 ) ) ,
52
- Direction :: South if row < self . height - 1 => Some ( Point :: new ( row + 1 , col) ) ,
53
- Direction :: East if col < self . width - 1 => Some ( Point :: new ( row, col + 1 ) ) ,
54
- _ => None
55
- }
47
+ DIRECTIONS . iter ( ) . filter_map ( move |d| match d {
48
+ Direction :: North if row > 0 => Some ( Point :: new ( row - 1 , col) ) ,
49
+ Direction :: West if col > 0 => Some ( Point :: new ( row, col - 1 ) ) ,
50
+ Direction :: South if row < self . height - 1 => Some ( Point :: new ( row + 1 , col) ) ,
51
+ Direction :: East if col < self . width - 1 => Some ( Point :: new ( row, col + 1 ) ) ,
52
+ _ => None ,
56
53
} )
57
54
}
58
55
59
- /// Perform BFS but skew the resulting path so
60
- /// equal Manhattan distances will be in an
56
+ /// Perform BFS but skew the resulting path so
57
+ /// equal Manhattan distances will be in an
61
58
/// axis-aligned box.
62
59
pub fn bfs ( & mut self ) -> Option < Vec < ( ( isize , isize ) , usize ) > > {
63
60
let mut seen = HashSet :: new ( ) ;
@@ -69,11 +66,11 @@ impl Map {
69
66
self . matrix [ p. row ] [ p. col ] = Some ( dist) ;
70
67
71
68
path. push ( ( p. to_uv ( ) , dist) ) ;
72
- if p == self . end {
73
- return Some ( path) ;
69
+ if p == self . end {
70
+ return Some ( path) ;
74
71
}
75
72
for neighbor in self . neighbors ( & p) {
76
- if !seen. contains ( & ( neighbor) ) && self . get ( & neighbor) . is_some ( ) {
73
+ if !seen. contains ( & ( neighbor) ) && self . get ( & neighbor) . is_some ( ) {
77
74
seen. insert ( neighbor) ;
78
75
queue. push_back ( ( neighbor, dist + 1 ) ) ;
79
76
}
@@ -84,30 +81,31 @@ impl Map {
84
81
}
85
82
86
83
impl From < & String > for Map {
87
- fn from ( s : & String ) -> Self {
84
+ fn from ( s : & String ) -> Self {
88
85
let mut start = None ;
89
86
let mut end = None ;
90
- let m: Vec < Vec < Option < usize > > > = s. split ( "\n " )
91
- . enumerate ( )
92
- . map ( |( row, line) | {
93
- line. chars ( )
87
+ let m: Vec < Vec < Option < usize > > > = s
88
+ . split ( "\n " )
94
89
. enumerate ( )
95
- . map ( |( col, c) | {
96
- match c {
97
- '#' => None ,
98
- '.' => Some ( usize:: MAX ) ,
99
- 'S' => {
100
- start = Some ( Point { row, col} ) ;
101
- Some ( 0 )
102
- } ,
103
- 'E' => {
104
- end = Some ( Point { row, col} ) ;
105
- Some ( usize:: MAX )
106
- }
107
- _ => panic ! ( "Debis on the field!!" )
108
- }
109
- } ) . collect ( )
110
- } ) . collect ( ) ;
90
+ . map ( |( row, line) | {
91
+ line. chars ( )
92
+ . enumerate ( )
93
+ . map ( |( col, c) | match c {
94
+ '#' => None ,
95
+ '.' => Some ( usize:: MAX ) ,
96
+ 'S' => {
97
+ start = Some ( Point { row, col } ) ;
98
+ Some ( 0 )
99
+ }
100
+ 'E' => {
101
+ end = Some ( Point { row, col } ) ;
102
+ Some ( usize:: MAX )
103
+ }
104
+ _ => panic ! ( "Debis on the field!!" ) ,
105
+ } )
106
+ . collect ( )
107
+ } )
108
+ . collect ( ) ;
111
109
Map {
112
110
start : start. unwrap ( ) ,
113
111
end : end. unwrap ( ) ,
0 commit comments