1
- use std:: u32;
2
-
3
- use crate :: grid:: { iter_neighbour4, Color , Grid } ;
1
+ use crate :: grid:: { iter_neighbour4, Color , Grid , Point } ;
4
2
5
3
pub fn get_exit_cost_grid ( color_grid : & Grid < Color > ) -> Grid < u32 > {
6
4
let mut exit_cost_grid = Grid :: < u32 > :: create ( color_grid. width , color_grid. height ) ;
@@ -19,7 +17,12 @@ pub fn get_exit_cost_grid(color_grid: &Grid<Color>) -> Grid<u32> {
19
17
} )
20
18
. min ( )
21
19
. unwrap ( ) ;
22
- let self_cost = ( 10 as u32 ) . pow ( color_grid. get ( & p) as u32 ) ;
20
+
21
+ let self_cost = match color_grid. get ( & p) {
22
+ Color :: Empty => 0 ,
23
+ c => ( 256 as u32 ) . pow ( ( c as u32 ) - 1 ) ,
24
+ } ;
25
+ println ! ( "color: {:?} cost:{}" , color_grid. get( & p) , self_cost) ;
23
26
24
27
let cost = neighbour_cost_min + self_cost;
25
28
@@ -36,3 +39,61 @@ pub fn get_exit_cost_grid(color_grid: &Grid<Color>) -> Grid<u32> {
36
39
37
40
exit_cost_grid
38
41
}
42
+
43
+ #[ test]
44
+ fn it_should_compute_exist_cost_grid_1 ( ) {
45
+ let color_grid = Grid :: < Color > :: from (
46
+ r#"
47
+ _._
48
+ "# ,
49
+ ) ;
50
+ let exit_grid = get_exit_cost_grid ( & color_grid) ;
51
+
52
+ assert_eq ! (
53
+ exit_grid. to_string( ) ,
54
+ r#"
55
+ 010
56
+ "#
57
+ . trim( ) ,
58
+ )
59
+ }
60
+
61
+ #[ test]
62
+ fn it_should_compute_exist_cost_grid_2 ( ) {
63
+ let color_grid = Grid :: < Color > :: from (
64
+ r#"
65
+ _ .... _
66
+ . ......
67
+ . ... .
68
+ ....
69
+ "# ,
70
+ ) ;
71
+ let exit_grid = get_exit_cost_grid ( & color_grid) ;
72
+
73
+ assert_eq ! (
74
+ exit_grid. to_string( ) ,
75
+ r#"
76
+ 0011110000000
77
+ 0111111111110
78
+ 0111111110010
79
+ 0011110000000
80
+ "#
81
+ . trim( ) ,
82
+ )
83
+ }
84
+
85
+ #[ test]
86
+ fn it_should_compute_exist_cost_grid_3 ( ) {
87
+ let color_grid = Grid :: < Color > :: from (
88
+ r#"
89
+ #####
90
+ #####
91
+ ## ##
92
+ #####
93
+ #####
94
+ "# ,
95
+ ) ;
96
+ let exit_grid = get_exit_cost_grid ( & color_grid) ;
97
+
98
+ assert_eq ! ( exit_grid. get( & Point { x: 2 , y: 2 } ) , 256 * 256 * 256 * 2 )
99
+ }
0 commit comments