1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
pub struct Grid {
pub width: usize,
pub height: usize,
pub costs: Vec<Vec<u32>>,
}
impl Grid {
pub fn from(grid: &[&[u32]]) -> Grid {
let (width, height) = (grid.len(), grid[0].len());
let mut costs = vec![vec![0; height]; width];
for (row, row_value) in grid.iter().enumerate() {
for (col, col_value) in row_value.iter().enumerate() {
costs[row][col] = col_value.clone();
}
}
return Grid {
width,
height,
costs,
};
}
pub fn outside(&self, coord: (usize, usize)) -> bool {
return !self.within(coord);
}
pub fn within(&self, coord: (usize, usize)) -> bool {
return coord.0 < self.costs.len() && coord.1 < self.costs[coord.0].len();
}
}
#[test]
fn coord_should_be_within() {
let coord: (usize, usize) = (0, 0);
let grid = Grid::from(&[
&[4, 2, 1],
&[2, 1, 0]
]);
assert!(grid.within(coord));
assert!(!grid.outside(coord));
}
#[test]
fn coord_row_should_be_outside() {
let coord: (usize, usize) = (2, 0);
let grid = Grid::from(&[
&[4, 2, 1],
&[2, 1, 0]
]);
assert!(grid.outside(coord));
}
#[test]
fn coord_col_should_be_outside() {
let coord: (usize, usize) = (0, 3);
let grid = Grid::from(&[
&[4, 2, 1],
&[2, 1, 0]
]);
assert!(grid.outside(coord));
}
#[test]
fn from_should_create_grid() {
let grid_matrix: &[&[u32]] = &[
&[0, 4, 0, 0, 0, 0, 0, 8, 0],
&[4, 0, 8, 0, 0, 0, 0, 11, 0],
&[0, 8, 0, 7, 0, 4, 0, 0, 2],
&[0, 0, 7, 0, 9, 14, 0, 0, 0],
&[0, 0, 0, 9, 0, 10, 0, 0, 0],
&[0, 0, 4, 14, 10, 0, 2, 0, 0],
&[0, 0, 0, 0, 0, 2, 0, 1, 6],
&[8, 11, 0, 0, 0, 0, 1, 0, 7],
&[0, 0, 2, 0, 0, 0, 6, 7, 0]
];
let grid = Grid::from(grid_matrix);
assert_eq!(9, grid.height);
assert_eq!(9, grid.width);
assert_eq!(7, grid.costs[8][7]);
}