1- use std:: collections:: { VecDeque , HashSet } ;
1+ use std:: collections:: { HashSet , VecDeque } ;
22
33pub enum Direction {
44 North ,
55 East ,
66 South ,
7- West
7+ West ,
88}
99
10- const DIRECTIONS : [ Direction ; 4 ] = [
11- Direction :: North ,
10+ const DIRECTIONS : [ Direction ; 4 ] = [
11+ Direction :: North ,
1212 Direction :: East ,
1313 Direction :: South ,
14- Direction :: West
14+ Direction :: West ,
1515] ;
1616
1717#[ derive( Debug , Hash , PartialEq , Eq , Clone , Copy ) ]
18- pub struct Point {
18+ pub struct Point {
1919 row : usize ,
20- col : usize
20+ col : usize ,
2121}
2222impl 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 }
2525 }
2626 pub fn to_uv ( & self ) -> ( isize , isize ) {
2727 let ( row, col) = ( self . row as isize , self . col as isize ) ;
2828 ( row + col, row - col)
2929 }
3030}
3131
32- pub struct Map {
32+ pub struct Map {
3333 width : usize ,
3434 height : usize ,
3535 start : Point ,
3636 end : Point ,
37- pub matrix : Vec < Vec < Option < usize > > >
37+ pub matrix : Vec < Vec < Option < usize > > > ,
3838}
3939
4040impl Map {
41- fn get ( & self , p : & Point ) -> Option < usize > {
41+ fn get ( & self , p : & Point ) -> Option < usize > {
4242 self . matrix [ p. row ] [ p. col ]
4343 }
4444
4545 pub fn neighbors ( & self , p : & Point ) -> impl Iterator < Item = Point > + ' _ {
4646 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 ,
5653 } )
5754 }
5855
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
6158 /// axis-aligned box.
6259 pub fn bfs ( & mut self ) -> Option < Vec < ( ( isize , isize ) , usize ) > > {
6360 let mut seen = HashSet :: new ( ) ;
@@ -69,11 +66,11 @@ impl Map {
6966 self . matrix [ p. row ] [ p. col ] = Some ( dist) ;
7067
7168 path. push ( ( p. to_uv ( ) , dist) ) ;
72- if p == self . end {
73- return Some ( path) ;
69+ if p == self . end {
70+ return Some ( path) ;
7471 }
7572 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 ( ) {
7774 seen. insert ( neighbor) ;
7875 queue. push_back ( ( neighbor, dist + 1 ) ) ;
7976 }
@@ -84,30 +81,31 @@ impl Map {
8481}
8582
8683impl From < & String > for Map {
87- fn from ( s : & String ) -> Self {
84+ fn from ( s : & String ) -> Self {
8885 let mut start = None ;
8986 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 " )
9489 . 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 ( ) ;
111109 Map {
112110 start : start. unwrap ( ) ,
113111 end : end. unwrap ( ) ,
0 commit comments