-
Notifications
You must be signed in to change notification settings - Fork 0
/
solver.cpp
126 lines (105 loc) · 3.11 KB
/
solver.cpp
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//
// Created by Dion on 26/10/2015.
//
#include "sudoku.h"
using namespace std;
bool sudoku::soleCandidate() {
bool toReturn = false;
convolve(this, [&toReturn](sudoku* puzzle, int y, int x){
int candidate = 0;
if(puzzle->solution[y][x] != 0){
return false;
}
for(int k = 0; k < N; k++){
if(puzzle->candidates[y][x][k]){
if(candidate == 0){
candidate = k + 1;
}else{
candidate = 0;
break;
}
}
}
if(candidate != 0) {
puzzle->makeMove("soleCandidate", y, x, candidate);
return true;
}
return false;
});
return toReturn;
}
bool sudoku::singleLocationInRow(){
bool toReturn = false;
int fitsCol = -1;
for(int row = 0; row < 9; row++){
for(int k = 0; k < 9; k++ ){
fitsCol = -1;
for(int col = 0; col < 9; col++){
if (this->candidates[row][col][k]) {
if (fitsCol == -1) {
fitsCol = col;
}else{
fitsCol = -2;
}
}
}
if (fitsCol >= 0) {
this->makeMove("singleLocationInRow", row, fitsCol, k+1);
toReturn = true;
}
}
}
return toReturn;
};
bool sudoku::singleLocationInColumn(){
bool toReturn = false;
int fitsRow = -1;
for (int col = 0; col < 9; col++){
for (int k = 0; k < 9; k++){
fitsRow = -1;
for (int row = 0; row < 9; row++){
if (this->candidates[row][col][k]){
if (fitsRow == -1){
fitsRow = row;
}else{
fitsRow = -2;
}
}
}
if (fitsRow >= 0){
this->makeMove("singleLocationInColumn", fitsRow, col, k + 1);
toReturn = true;
}
}
}
return toReturn;
};
bool sudoku::singleLocationInBlock(){
bool toReturn = false;
int fitsCell = -1;
for(int blocky; blocky < 3; blocky++){
for(int blockx; blockx < 3; blockx++){
for(int k = 0; k < N; k++){
fitsCell = -1;
for(int celly; celly < 3; celly++){
for(int cellx; cellx < 3; cellx++){
if(this->candidates[blocky*3 + celly][blockx*3 + cellx][k]){
if(fitsCell == -1){
fitsCell = celly*3 + cellx;
}else{
fitsCell = -2;
}
}
}
}
if(fitsCell >= 0 ){
int y = blocky * 3 + fitsCell/3;
int x = blockx * 3 + fitsCell%3;
this->makeMove("singleLocationInBlock", y, x, k+1);
toReturn = true;
}
}
}
}
return toReturn;
};