-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSudoku.cpp
371 lines (340 loc) · 8.54 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#include "Sudoku.h"
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
// Definição de variáveis principais
int matriz_numeros_disponiveis[9][9][9], matriz_resposta[9][9], matriz_incompleta[9][9][3];
void SudokuCreation(int dificuldade);
void GerarResposta();
void NumAleatorio(int i, int j);
void NumCerto();
int* Quadrado(int i, int j);
void NumerosDisponiveis(int i, int j, int resposta, int quadrado[2]);
void Recalculate();
int CheckPlace(int i, int j);
void Backtrack(int i, int j);
void Remover(int dificuldade);
int Resolver();
void SudokuCreation(int dificuldade) {
srand(time(NULL));
// Certo 1 = Sudoku valido
int certo = 0;
int n = 0;
// Geração de matrizes até o retorno de uma válida
while (certo == 0) {
certo = 1;
GerarResposta();
// Checagem da validade da resposta
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (matriz_resposta[i][j] == 0) {
certo = 0;
}
}
}
n++;
}
Remover(dificuldade);
// Atribuir a condição "alterável" (0) à matriz incompleta
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (matriz_incompleta[i][j][0] == 0)
matriz_incompleta[i][j][1] = 0;
else
matriz_incompleta[i][j][1] = 1;
}
}
}
void GerarResposta() {
// Definição inicial de todos os números disponíveis
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
for (int k = 0; k < 9; k++) {
matriz_numeros_disponiveis[i][j][k] = k + 1;
}
matriz_resposta[i][j] = 0;
}
}
// Criação da matriz resposta original do sudoku
// Criação de quadrados da diagonal principal, limitando as respostas
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
NumAleatorio(i, j);
}
}
for (int i = 3; i < 6; i++) {
for (int j = 3; j < 6; j++) {
NumAleatorio(i, j);
}
}
for (int i = 6; i < 9; i++) {
for (int j = 6; j < 9; j++) {
NumAleatorio(i, j);
}
}
// Determinação das casas restantes
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
// Tomada de numeros aleatorios para as respostas iniciais
if (matriz_resposta[i][j] == 0) {
NumAleatorio(i, j);
}
// Determinacao de valores ja certos
NumCerto();
}
}
}
void NumAleatorio(int i, int j) {
// Definição inicial de variáveis
int* quadrado;
int o = 0;
int index[9], h = 0;
int resposta = 0;
// Determinação do número a ser atribuído
while (h == 0 && o < 16) {
for (int n = 0; n < 9; n++) {
if (matriz_numeros_disponiveis[i][j][n] != 0) {
index[h] = n;
h++;
}
}
// Atribuir número caso disponível
if (h > 0) {
resposta = index[rand() % h];
}
// Alterar números anteriores, caso não
else {
Backtrack(i, j);
o++;
}
}
// Determinação do quadrado 3x3 a que o ponto pertence
quadrado = Quadrado(i, j);
// Atribuição da resposta obtida
matriz_resposta[i][j] = matriz_numeros_disponiveis[i][j][resposta % 9];
// Retirada do número escolhido das matrizes de números disponíveis
NumerosDisponiveis(i, j, resposta, quadrado);
}
// Atribuição de resposta a casas com apenas um número disponível
void NumCerto() {
int necessidade = 1;
while (necessidade > 0) {
necessidade = 81;
for (int x = 0; x < 9; x++) {
for (int y = 0; y < 9; y++) {
int num = 0;
for (int resp = 0; resp < 9; resp++) {
if (matriz_numeros_disponiveis[x][y][resp] != 0 && matriz_resposta[x][y] == 0) {
num++;
}
}
if (num == 1) {
for (int resp = 0; resp < 9; resp++) {
if (matriz_numeros_disponiveis[x][y][resp] != 0) {
matriz_resposta[x][y] = matriz_numeros_disponiveis[x][y][resp];
NumerosDisponiveis(x, y, resp, Quadrado(x, y));
}
}
}
else {
necessidade--;
}
}
}
}
}
// Determinação da coordenada a que uma casa pertence
int* Quadrado(int i, int j) {
static int quadrado[2];
switch (i) {
case 3:
case 4:
case 5:
quadrado[0] = 1;
break;
case 6:
case 7:
case 8:
quadrado[0] = 2;
break;
default:
quadrado[0] = 0;
break;
}
switch (j) {
case 3:
case 4:
case 5:
quadrado[1] = 1;
break;
case 6:
case 7:
case 8:
quadrado[1] = 2;
break;
default:
quadrado[1] = 0;
break;
}
return quadrado;
}
// Retirada do número selecionado da matriz de números disponíveis
void NumerosDisponiveis(int i, int j, int resposta, int quadrado[2]) {
// Retirada da linha
for (int x = 0; x < 9; x++) {
matriz_numeros_disponiveis[i][x][resposta % 9] = 0;
}
// Retirada da coluna
for (int y = 0; y < 9; y++) {
matriz_numeros_disponiveis[y][j][resposta % 9] = 0;
}
// Retirada do quadrado
for (int x = quadrado[0] * 3; x < (quadrado[0] + 1) * 3; x++) {
for (int y = quadrado[1] * 3; y < (quadrado[1] + 1) * 3; y++) {
matriz_numeros_disponiveis[x][y][resposta % 9] = 0;
}
}
}
// Recalcula a matriz de números disponíveis
void Recalculate() {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
for (int k = 0; k < 9; k++) {
matriz_numeros_disponiveis[i][j][k] = k + 1;
}
}
}
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (matriz_resposta[i][j] != 0) {
int* quadrado = Quadrado(i, j);
NumerosDisponiveis(i, j, matriz_resposta[i][j] - 1, quadrado);
}
}
}
}
// Checa a existência de números disponíveis em uma posição
int CheckPlace(int i, int j) {
for (int x = 0; x < 9; x++) {
if (matriz_numeros_disponiveis[i][j][x] != 0) {
return 1;
}
}
return 0;
}
// Busca casas anteriores da matriz que podem ter seu valor alterado
void Backtrack(int i, int j) {
int y = i;
int x = 8;
int resp = 0;
while (y >= 0 && CheckPlace(i, j) == 0) {
while (x >= 0 && CheckPlace(i, j) == 0) {
while (resp < 9 && CheckPlace(i, j) == 0) {
if (matriz_numeros_disponiveis[y][x][resp] != 0 && resp + 1 != matriz_resposta[y][x] && matriz_resposta[x][y] != 0) {
matriz_resposta[y][x] = matriz_numeros_disponiveis[y][x][resp];
Recalculate();
}
resp++;
}
x--;
}
y--;
}
}
// Criação da matriz final do sudoku
void Remover(int dificuldade) {
int index[81];
// Atribuição inicial da matriz
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
matriz_incompleta[i][j][0] = matriz_resposta[i][j];
matriz_incompleta[i][j][2] = matriz_resposta[i][j];
}
}
// Remoção de números da matriz
int n = 0;
while (n < dificuldade) {
int h = 0;
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (matriz_incompleta[i][j][2] != 0) {
index[h] = i * 9 + j;
h++;
}
}
}
if (h == 0) {
break;
}
int pos = index[rand() % h];
// Definição da matriz a ser testada
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
matriz_incompleta[i][j][1] = matriz_incompleta[i][j][0];
}
}
matriz_incompleta[(pos - pos % 9) / 9][pos % 9][1] = 0;
// Remoção do número escolhido caso possível
if (Resolver() == 1) {
matriz_incompleta[(pos - pos % 9) / 9][pos % 9][0] = 0;
matriz_incompleta[(pos - pos % 9) / 9][pos % 9][2] = 0;
n++;
}
else {
matriz_incompleta[(pos - pos % 9) / 9][pos % 9][2] = 0;
}
}
}
int Resolver() {
// Criação da matriz de números disponíveis
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
for (int k = 0; k < 9; k++) {
matriz_numeros_disponiveis[i][j][k] = k + 1;
}
}
}
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (matriz_incompleta[i][j][1] != 0) {
int* quadrado = Quadrado(i, j);
NumerosDisponiveis(i, j, matriz_incompleta[i][j][1] - 1, quadrado);
}
}
}
// Resolução da matriz incompleta
int necessidade = 1;
while (necessidade > 0) {
necessidade = 81;
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
int num = 0;
for (int resp = 0; resp < 9; resp++) {
if (matriz_numeros_disponiveis[i][j][resp] != 0 && matriz_incompleta[i][j][1] == 0) {
num++;
}
}
if (num == 1) {
for (int resp = 0; resp < 9; resp++) {
if (matriz_numeros_disponiveis[i][j][resp] != 0) {
matriz_incompleta[i][j][1] = matriz_numeros_disponiveis[i][j][resp];
NumerosDisponiveis(i, j, resp, Quadrado(i, j));
}
}
}
else {
necessidade--;
}
}
}
}
// Checa se a matriz resolvida está completa
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (matriz_incompleta[i][j][1] == 0)
return 0;
}
}
return 1;
}