|
@@ -1,6 +1,5 @@
|
1
|
1
|
using System;
|
2
|
2
|
using System.Collections.Generic;
|
3
|
|
-using System.Linq;
|
4
|
3
|
using uqac_ia_sudoku_csp.Interfaces;
|
5
|
4
|
using uqac_ia_sudoku_csp.Solver.Constraints;
|
6
|
5
|
|
|
@@ -9,11 +8,13 @@ namespace uqac_ia_sudoku_csp.Solver
|
9
|
8
|
public class BacktrackSearch : ISolver
|
10
|
9
|
{
|
11
|
10
|
private readonly INextCellChooser _nextCellChooser;
|
|
11
|
+ private readonly INextNumberChooser _nextNumberChooser;
|
12
|
12
|
protected IList<IConstraint> Constraints;
|
13
|
13
|
|
14
|
|
- public BacktrackSearch(INextCellChooser nextCellChooser)
|
|
14
|
+ public BacktrackSearch(INextCellChooser nextCellChooser, INextNumberChooser nextNumberChooser)
|
15
|
15
|
{
|
16
|
16
|
_nextCellChooser = nextCellChooser;
|
|
17
|
+ _nextNumberChooser = nextNumberChooser;
|
17
|
18
|
Constraints = new List<IConstraint>
|
18
|
19
|
{
|
19
|
20
|
new LineContraint(),
|
|
@@ -24,16 +25,15 @@ namespace uqac_ia_sudoku_csp.Solver
|
24
|
25
|
|
25
|
26
|
public SolverResult Resolve(Board board)
|
26
|
27
|
{
|
27
|
|
- var domain = Enumerable.Range(0, board.Size).ToList();
|
28
|
28
|
var result = new SolverResult();
|
29
|
29
|
var start = DateTime.Now;
|
30
|
|
- result.Success = RecursiveResolve(board, result, domain);
|
|
30
|
+ result.Success = RecursiveResolve(board, result);
|
31
|
31
|
var end = DateTime.Now;
|
32
|
32
|
result.Elapsed = end - start;
|
33
|
33
|
return result;
|
34
|
34
|
}
|
35
|
35
|
|
36
|
|
- protected bool RecursiveResolve(Board board, SolverResult result, IList<int> domain)
|
|
36
|
+ protected bool RecursiveResolve(Board board, SolverResult result)
|
37
|
37
|
{
|
38
|
38
|
if (board.IsComplete())
|
39
|
39
|
{
|
|
@@ -43,14 +43,14 @@ namespace uqac_ia_sudoku_csp.Solver
|
43
|
43
|
_nextCellChooser.SelectVariable(board, out x, out y, Constraints);
|
44
|
44
|
if (x != -1 && y != -1)
|
45
|
45
|
{
|
46
|
|
- foreach (var value in domain)
|
|
46
|
+ foreach (var value in _nextNumberChooser.ChooseNext(board, x, y))
|
47
|
47
|
{
|
48
|
48
|
++result.TryCount;
|
49
|
49
|
board.SetNumber(x, y, value);
|
50
|
50
|
if (board.IsConsistent(x, y, Constraints))
|
51
|
51
|
{
|
52
|
52
|
++result.ConsistentTryCount;
|
53
|
|
- if (RecursiveResolve(board, result, domain))
|
|
53
|
+ if (RecursiveResolve(board, result))
|
54
|
54
|
{
|
55
|
55
|
return true;
|
56
|
56
|
}
|