-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f5d9e3b
commit cdde051
Showing
9 changed files
with
310 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use criterion::{criterion_group, criterion_main, Criterion}; | ||
use sudoku::{ | ||
game::generator::random_sudoku_puzzle_normal, | ||
neo::{ | ||
puzzle::SudokuPuzzleSimple, | ||
solver::{Solver, StochasticSolver}, | ||
}, | ||
}; | ||
|
||
fn benchmarks(c: &mut Criterion) { | ||
let puzzle = random_sudoku_puzzle_normal(); | ||
let mut solver = StochasticSolver::<SudokuPuzzleSimple>::new(puzzle); | ||
c.bench_function("StochasticSolver", |b| { | ||
b.iter(|| { | ||
solver.any_solution(); | ||
}) | ||
}); | ||
} | ||
|
||
criterion_group!(benches, benchmarks); | ||
criterion_main!(benches); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use super::utils::coord_2_block; | ||
|
||
// 返回 ( | ||
// board是否为有效的部分解 | ||
// board是否为有效的完全解 | ||
// board中违反约束的格子 | ||
// ) | ||
pub fn judge_sudoku(board: &[[i8; 9]; 9]) -> (bool, bool, [[bool; 9]; 9]) { | ||
let mut row: [[(i8, i8); 10]; 9] = [[(-1, -1); 10]; 9]; | ||
let mut col: [[(i8, i8); 10]; 9] = [[(-1, -1); 10]; 9]; | ||
let mut block: [[(i8, i8); 10]; 9] = [[(-1, -1); 10]; 9]; | ||
let mut valid = true; | ||
let mut full = true; | ||
let mut valid_cond = [[true; 9]; 9]; | ||
for r in 0..9 { | ||
for c in 0..9 { | ||
if board[r][c] > 0 { | ||
let b = coord_2_block(r, c); | ||
if row[r][board[r][c] as usize] != (-1, -1) { | ||
valid = false; | ||
valid_cond[r][c] = false; | ||
let (r1, c1) = row[r][board[r][c] as usize]; | ||
valid_cond[r1 as usize][c1 as usize] = false; | ||
} | ||
if col[c][board[r][c] as usize] != (-1, -1) { | ||
valid = false; | ||
valid_cond[r][c] = false; | ||
let (r1, c1) = col[c][board[r][c] as usize]; | ||
valid_cond[r1 as usize][c1 as usize] = false; | ||
} | ||
if block[b][board[r][c] as usize] != (-1, -1) { | ||
valid = false; | ||
valid_cond[r][c] = false; | ||
let (r1, c1) = block[b][board[r][c] as usize]; | ||
valid_cond[r1 as usize][c1 as usize] = false; | ||
} | ||
row[r][board[r][c] as usize] = (r as i8, c as i8); | ||
col[c][board[r][c] as usize] = (r as i8, c as i8); | ||
block[b][board[r][c] as usize] = (r as i8, c as i8); | ||
} else { | ||
full = false; | ||
} | ||
} | ||
} | ||
(valid, valid && full, valid_cond) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
use super::puzzle::{Fillable, Grid, TrackingCandidates}; | ||
use rand::prelude::*; | ||
|
||
pub trait Solver { | ||
fn new(puzzle: [[i8; 9]; 9]) -> Self; | ||
fn any_solution(&mut self) -> Option<[[i8; 9]; 9]>; | ||
fn solution_cnt(&mut self) -> u32; | ||
fn have_unique_solution(&mut self) -> bool; | ||
} | ||
|
||
pub struct StochasticSolver<T> | ||
where | ||
T: Grid + Fillable + TrackingCandidates, | ||
{ | ||
puzzle_arr: [[i8; 9]; 9], | ||
puzzle: T, | ||
solution_cnt: u32, | ||
} | ||
|
||
impl<T> StochasticSolver<T> | ||
where | ||
T: Grid + Fillable + TrackingCandidates, | ||
{ | ||
fn init_search(&mut self) { | ||
self.solution_cnt = 0; | ||
self.puzzle = T::new(self.puzzle_arr); | ||
} | ||
|
||
fn next_blank(&self, mut row: usize, mut col: usize) -> Option<(usize, usize)> { | ||
while row < 9 && !self.puzzle.is_grid_empty(row, col) { | ||
if col == 8 { | ||
col = 0; | ||
row += 1; | ||
} else { | ||
col += 1 | ||
} | ||
} | ||
if row == 9 { | ||
return None; | ||
} | ||
Some((row, col)) | ||
} | ||
|
||
fn search(&mut self, r: usize, c: usize, solution_cnt_needed: u32) -> bool { | ||
let coord = self.next_blank(r, c); | ||
if coord.is_none() { | ||
self.solution_cnt += 1; | ||
return true; | ||
} | ||
let (r, c) = coord.unwrap(); | ||
|
||
let mut nums: Vec<i8> = (1..=9).collect(); | ||
nums.shuffle(&mut rand::thread_rng()); | ||
for num in nums { | ||
if self.puzzle.is_candidate_of(r, c, num) { | ||
self.puzzle.fill_grid(r, c, num); | ||
|
||
if self.search(r, c, solution_cnt_needed) | ||
&& solution_cnt_needed <= self.solution_cnt | ||
{ | ||
return true; | ||
} | ||
|
||
self.puzzle.unfill_grid(r, c); | ||
} | ||
} | ||
|
||
false | ||
} | ||
} | ||
|
||
impl<T> Solver for StochasticSolver<T> | ||
where | ||
T: Grid + Fillable + TrackingCandidates, | ||
{ | ||
fn new(puzzle: [[i8; 9]; 9]) -> Self { | ||
Self { | ||
puzzle_arr: puzzle, | ||
puzzle: T::new(puzzle), | ||
solution_cnt: 0, | ||
} | ||
} | ||
|
||
fn any_solution(&mut self) -> Option<[[i8; 9]; 9]> { | ||
self.init_search(); | ||
if self.search(0, 0, 1) { | ||
return Some(self.puzzle.board()); | ||
} | ||
None | ||
} | ||
|
||
fn solution_cnt(&mut self) -> u32 { | ||
self.init_search(); | ||
self.search(0, 0, u32::MAX); | ||
self.solution_cnt | ||
} | ||
|
||
fn have_unique_solution(&mut self) -> bool { | ||
self.init_search(); | ||
self.search(0, 0, 2); | ||
self.solution_cnt == 1 | ||
} | ||
} |
Oops, something went wrong.