-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.rs
106 lines (101 loc) · 3.49 KB
/
main.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
fn main() {
let x = vec!["5","3",".",".","7",".",".",".","."];
println!("x = {:#?}", x);
let y = x.into_iter().map(|x| x.chars().nth(0).unwrap()).collect::<Vec<char>>();
println!("x = {:#?}", y);
for &ibase in &[0,3,6] {
println!("ibase = {:#?}", ibase);
}
}
struct Solution {}
impl Solution {
pub fn is_valid_sudoku(board: Vec<Vec<char>>) -> bool {
let mut buf = vec![0; 10];
// line
for i in 0..9 {
for idx in 1..10 { buf[idx] = 0 }
for j in 0..9 {
if board[i][j] != '.' {
let idx = board[i][j].to_digit(10).unwrap() as usize;
if buf[idx] > 0 { return false }
else { buf[idx] += 1 }
}
}
}
// coloum
for j in 0..9 {
for idx in 1..10 { buf[idx] = 0 }
for i in 0..9 {
if board[i][j] != '.' {
let idx = board[i][j].to_digit(10).unwrap() as usize;
if buf[idx] > 0 { return false }
else { buf[idx] += 1 }
}
}
}
// square
for &ibase in &[0,3,6] {
for &jbase in &[0,3,6] {
for idx in 1..10 { buf[idx] = 0 }
for idx in 0..3 {
for jdx in 0..3 {
let i = idx + ibase;
let j = jdx + jbase;
if board[i][j] != '.' {
let idx = board[i][j].to_digit(10).unwrap() as usize;
if buf[idx] > 0 { return false }
else { buf[idx] += 1 }
}
}
}
}
}
true
}
}
#[cfg(test)]
mod test {
use crate::*;
#[test]
fn basic1() {
let board =
vec![
vec!["5","3",".",".","7",".",".",".","."],
vec!["6",".",".","1","9","5",".",".","."],
vec![".","9","8",".",".",".",".","6","."],
vec!["8",".",".",".","6",".",".",".","3"],
vec!["4",".",".","8",".","3",".",".","1"],
vec!["7",".",".",".","2",".",".",".","6"],
vec![".","6",".",".",".",".","2","8","."],
vec![".",".",".","4","1","9",".",".","5"],
vec![".",".",".",".","8",".",".","7","9"]
];
let board = board.into_iter().map(|vec| {
vec.into_iter().map(|s| {
s.chars().nth(0).unwrap()
}).collect()
}).collect();
assert_eq!(Solution::is_valid_sudoku(board), true);
}
#[test]
fn basic2() {
let board =
vec![
vec!["8","3",".",".","7",".",".",".","."],
vec!["6",".",".","1","9","5",".",".","."],
vec![".","9","8",".",".",".",".","6","."],
vec!["8",".",".",".","6",".",".",".","3"],
vec!["4",".",".","8",".","3",".",".","1"],
vec!["7",".",".",".","2",".",".",".","6"],
vec![".","6",".",".",".",".","2","8","."],
vec![".",".",".","4","1","9",".",".","5"],
vec![".",".",".",".","8",".",".","7","9"]
];
let board = board.into_iter().map(|vec| {
vec.into_iter().map(|s| {
s.chars().nth(0).unwrap()
}).collect()
}).collect();
assert_eq!(Solution::is_valid_sudoku(board), false);
}
}