Skip to content

Commit a18c6af

Browse files
committed
solution: Sudoku Solver
1 parent 40a4e00 commit a18c6af

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ mod sort_array_by_parity;
2828
mod sort_array_by_parity_ii;
2929
mod sort_colors;
3030
mod string_to_integer_atoi;
31+
mod sudoku_solver;
3132
mod three_sum;
3233
mod three_sum_closest;
3334
mod to_lower_case;

src/sudoku_solver.rs

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/// @number 37
2+
/// @title Sudoku Solver
3+
/// @url https://leetcode.com/problems/sudoku-solver/
4+
/// @difficulty Hard
5+
6+
struct Solution;
7+
8+
impl Solution {
9+
pub fn solve_sudoku(board: &mut Vec<Vec<char>>) {
10+
let mut empty_position = vec![];
11+
for i in 0..9 {
12+
for j in 0..9 {
13+
if board[i][j] == '.' {
14+
empty_position.push((i, j));
15+
board[i][j] = '0';
16+
}
17+
}
18+
}
19+
let position_len = empty_position.len();
20+
21+
let mut loop_index = 0;
22+
while loop_index < position_len {
23+
let (x, y) = empty_position[loop_index];
24+
25+
if board[x][y] < '9' {
26+
board[x][y] = std::char::from_u32((board[x][y] as u32) + 1).unwrap();
27+
} else {
28+
board[x][y] = '0';
29+
loop_index -= 1;
30+
continue;
31+
}
32+
if Solution::invalid(board, x, y) {
33+
continue;
34+
}
35+
loop_index += 1;
36+
}
37+
}
38+
pub fn invalid(board: &Vec<Vec<char>>, x: usize, y: usize) -> bool {
39+
use std::collections::HashSet;
40+
let mut contain_table = HashSet::new();
41+
42+
// validate line
43+
for i in 0..9 {
44+
let x1 = board[x][i];
45+
if x1 != '0' {
46+
if contain_table.contains(&x1) {
47+
return true;
48+
}
49+
contain_table.insert(x1);
50+
}
51+
}
52+
contain_table.clear();
53+
54+
// validate column
55+
56+
for i in 0..9 {
57+
let x2 = board[i][y];
58+
if x2 != '0' {
59+
if contain_table.contains(&x2) {
60+
return true;
61+
}
62+
contain_table.insert(x2);
63+
}
64+
}
65+
66+
contain_table.clear();
67+
// validate box
68+
69+
let i1 = x / 3;
70+
let i2 = y / 3;
71+
for i in 0..3 {
72+
for j in 0..3 {
73+
let x3 = board[i1 * 3 + i][i2 * 3 + j];
74+
if x3 != '0' {
75+
if contain_table.contains(&x3) {
76+
return true;
77+
}
78+
contain_table.insert(x3);
79+
}
80+
}
81+
}
82+
contain_table.clear();
83+
false
84+
}
85+
}
86+
87+
#[cfg(test)]
88+
mod test {
89+
use crate::sudoku_solver::Solution;
90+
91+
#[test]
92+
fn test() {
93+
let mut sudoku = vec![
94+
vec!['5', '3', '.', '.', '7', '.', '.', '.', '.'],
95+
vec!['6', '.', '.', '1', '9', '5', '.', '.', '.'],
96+
vec!['.', '9', '8', '.', '.', '.', '.', '6', '.'],
97+
vec!['8', '.', '.', '.', '6', '.', '.', '.', '3'],
98+
vec!['4', '.', '.', '8', '.', '3', '.', '.', '1'],
99+
vec!['7', '.', '.', '.', '2', '.', '.', '.', '6'],
100+
vec!['.', '6', '.', '.', '.', '.', '2', '8', '.'],
101+
vec!['.', '.', '.', '4', '1', '9', '.', '.', '5'],
102+
vec!['.', '.', '.', '.', '8', '.', '.', '7', '9'],
103+
];
104+
Solution::solve_sudoku(&mut sudoku);
105+
106+
let mut expected_solution = vec![
107+
vec!['5', '3', '4', '6', '7', '8', '9', '1', '2'],
108+
vec!['6', '7', '2', '1', '9', '5', '3', '4', '8'],
109+
vec!['1', '9', '8', '3', '4', '2', '5', '6', '7'],
110+
vec!['8', '5', '9', '7', '6', '1', '4', '2', '3'],
111+
vec!['4', '2', '6', '8', '5', '3', '7', '9', '1'],
112+
vec!['7', '1', '3', '9', '2', '4', '8', '5', '6'],
113+
vec!['9', '6', '1', '5', '3', '7', '2', '8', '4'],
114+
vec!['2', '8', '7', '4', '1', '9', '6', '3', '5'],
115+
vec!['3', '4', '5', '2', '8', '6', '1', '7', '9'],
116+
];
117+
assert_eq!(expected_solution, sudoku);
118+
}
119+
120+
#[test]
121+
fn test_invalid_method() {
122+
let mut expected_solution = vec![
123+
vec!['5', '3', '4', '6', '7', '8', '9', '1', '2'],
124+
vec!['6', '7', '2', '1', '9', '5', '3', '4', '8'],
125+
vec!['1', '9', '8', '3', '4', '2', '5', '6', '7'],
126+
vec!['8', '5', '9', '7', '6', '1', '4', '2', '3'],
127+
vec!['4', '2', '6', '8', '5', '3', '7', '9', '1'],
128+
vec!['7', '1', '3', '9', '2', '4', '8', '5', '6'],
129+
vec!['9', '6', '1', '5', '3', '7', '2', '8', '4'],
130+
vec!['2', '8', '7', '4', '1', '9', '6', '3', '5'],
131+
vec!['3', '4', '5', '2', '8', '6', '1', '7', '9'],
132+
];
133+
for i in 0..9 {
134+
for j in 0..9 {
135+
assert_eq!(false, Solution::invalid(&expected_solution, i, j));
136+
}
137+
}
138+
}
139+
}

0 commit comments

Comments
 (0)