-
Notifications
You must be signed in to change notification settings - Fork 0
/
sudoku.cpp
74 lines (57 loc) · 1.61 KB
/
sudoku.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
//
// Created by Dion on 25/10/2015.
//
#include "sudoku.h"
using namespace std;
void sudoku::solve(int (&problem)[N][N]) {
memcpy(solution, problem, N * N * sizeof(int));
printSolution((int*)problem);
initCandidates();
// int x = 1;
// int y = 2;
// int c = 1;
//
// cout << "row: " << rowContains(y, c) << endl;
// cout << "column: " << columnContains(x, c) << endl;
// cout << "block: " << blockContains(y/R, x/R, c) << endl;
// printf("%i,%i:%i\n", y, x, this->solution[y][x]);
// printCandidates(y,x);
while(
this->soleCandidate()
|| this->singleLocationInRow()
|| this->singleLocationInColumn()
|| this->singleLocationInBlock()
// || this->isSolved()
){
}
this->printSolution((int*)this->solution);
// this->printCandidates(8,8);
// this->printCandidates(8,7);
// this->printCandidates(8,5);
}
void sudoku::initCandidates() {
convolve(this, [](sudoku* puzzle, int y, int x){
for (int c = 1; c < N + 1; c++) {
if (puzzle->solution[y][x] == 0) {
puzzle->calcCandidate(y, x, c);
}
}
return false;
});
return;
}
void sudoku::calcCandidate(int y, int x, int c) {
if (!(rowContains(y, c) || columnContains(x, c) || blockContains((y)/R, (x)/R, c) )){
this->candidates[y][x][c-1] = true;
}
}
bool sudoku::isSolved(){
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
if(this->solution[i][j] == 0){
return false;
}
}
}
return true;
}