-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix_01.rs
79 lines (62 loc) · 2.16 KB
/
matrix_01.rs
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
use std::{collections::VecDeque, usize};
#[derive(Debug, Default)]
struct Position {
x: i32,
y: i32,
}
pub fn update_matrix(matrix: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
let mut dist = vec![vec![std::i32::MAX; matrix[0].len()]; matrix.len()];
let mut queue: VecDeque<Position> = VecDeque::new();
for x in 0..matrix.len() {
for y in 0..matrix[0].len() {
if matrix[x][y] == 0 {
queue.push_back(Position {
x: x as i32,
y: y as i32,
});
dist[x][y] = 0;
}
}
}
let directions: Vec<Position> = vec![
Position { x: 0, y: 1 },
Position { x: 0, y: -1 },
Position { x: 1, y: 0 },
Position { x: -1, y: 0 },
];
while !queue.is_empty() {
let curr = queue.pop_front().unwrap();
for position in &directions {
let new_x = position.x + curr.x;
let new_y = position.y + curr.y;
if new_x < 0
|| new_x >= matrix.len() as i32
|| new_y < 0
|| new_y >= matrix[0].len() as i32
{
continue;
}
if dist[new_x as usize][new_y as usize] > dist[curr.x as usize][curr.y as usize] + 1 {
dist[new_x as usize][new_y as usize] = dist[curr.x as usize][curr.y as usize] + 1;
queue.push_back(Position { x: new_x, y: new_y });
}
}
}
return dist;
}
#[cfg(test)]
mod test_matrix_01_cont {
use super::*;
#[test]
fn test_matrix_01() {
let input = vec![vec![0, 0, 0], vec![0, 1, 0], vec![0, 0, 0]];
let output = vec![vec![0, 0, 0], vec![0, 1, 0], vec![0, 0, 0]];
assert_eq!(update_matrix(input), output);
let input = vec![vec![0, 0, 0], vec![0, 1, 0], vec![1, 1, 1]];
let output = vec![vec![0, 0, 0], vec![0, 1, 0], vec![1, 2, 1]];
assert_eq!(update_matrix(input), output);
let input = vec![vec![0], vec![0], vec![0], vec![0], vec![0]];
let output = vec![vec![0], vec![0], vec![0], vec![0], vec![0]];
assert_eq!(update_matrix(input), output);
}
}