-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsudokuSolver.c
162 lines (150 loc) · 5 KB
/
sudokuSolver.c
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <stdio.h>
void printSudoku(int s[9][9]) {
for (int y = 0; y < 9; y++) {
for (int x = 0; x < 9; x++) {
printf("%d, ", s[y][x]);
}
printf("\n");
}
printf("\n");
}
int verifySudoku(int s[9][9]) {
for (int y = 0; y < 9; y++) {
for (int x = 0; x < 9; x++) {
if (s[y][x] == 0) {
return 0;
}
}
}
return 1; //0 and 1 as boolean substitutes
}
int possible(int y, int x, int n, int s[9][9]) {
for (int i = 0; i < 9; i++) { //first we check both axes
if (s[y][i] == n) {
return 0;
}
}
for (int i = 0; i < 9; i++) {
if (s[i][x] == n) {
return 0;
}
}
int yBlock = (y / 3) * 3; //here we calculate the block that we are probing, and check all cells within it
int xBlock = (x / 3) * 3;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (s[yBlock + i][xBlock + j] == n) {
return 0;
}
}
}
return 1;
}
int **solve(int s[9][9]) {
/* This method works by iterating over the field, until it encounters a zero, then it tries the
* 9 numbers that could be there, for each it attempts it, then it passes the new field to the method again, should there
* at some point be an error, it sets the attempted cell back to zero and abandons this call by returning the unaltered field (backtracking).
* If the verification succeeds, we return the current field, as this is the solution.*/
for (int y = 0; y < 9; y++) {
for (int x = 0; x < 9; x++) {
if (s[y][x] == 0) {
for (int n = 1; n < 10; n++) {
if (possible(y, x, n, s)) {
s[y][x] = n;
// printSudoku(s);
solve(s);
if (verifySudoku(s)) {
return s;
} else {
s[y][x] = 0;
}
}
}
return s;
}
}
}
return s;
}
int main() {
int sudoku1[9][9] = { //Here are seven sudokus to test the program on
{9, 0, 0, 1, 0, 0, 0, 0, 5},
{0, 0, 5, 0, 9, 0, 2, 0, 1},
{8, 0, 0, 0, 4, 0, 0, 0, 0},
{0, 0, 0, 0, 8, 0, 0, 0, 0},
{0, 0, 0, 7, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 2, 6, 0, 0, 9},
{2, 0, 0, 3, 0, 0, 0, 0, 6},
{0, 0, 0, 2, 0, 0, 9, 0, 0},
{0, 0, 1, 9, 0, 4, 5, 7, 0},
};
int sudoku2[9][9] = {
{3, 1, 6, 5, 7, 8, 4, 9, 2},
{5, 0, 0, 1, 3, 4, 7, 6, 8},
{4, 0, 7, 0, 2, 9, 5, 3, 1},
{2, 6, 3, 0, 1, 5, 9, 0, 7},
{9, 7, 4, 8, 6, 0, 1, 2, 5},
{8, 5, 1, 7, 0, 2, 6, 4, 3},
{1, 0, 8, 0, 4, 7, 2, 0, 6},
{6, 9, 2, 3, 0, 1, 8, 7, 4},
{0, 4, 5, 0, 8, 6, 0, 1, 0}
};
int sudoku3[9][9] = {
{3, 0, 6, 5, 0, 8, 4, 0, 0},
{5, 2, 0, 0, 0, 0, 0, 0, 0},
{0, 8, 7, 0, 0, 0, 0, 3, 1},
{0, 0, 3, 0, 1, 0, 0, 8, 0},
{9, 0, 0, 8, 6, 3, 0, 0, 5},
{0, 5, 0, 0, 9, 0, 6, 0, 0},
{1, 3, 0, 0, 0, 0, 2, 5, 0},
{0, 0, 0, 0, 0, 0, 0, 7, 4},
{0, 0, 5, 2, 0, 6, 3, 0, 0}
};
int sudoku4[9][9] = {
{5, 8, 0, 2, 0, 0, 4, 7, 0},
{0, 2, 0, 0, 0, 0, 0, 3, 0},
{0, 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