-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsudoku.h
252 lines (215 loc) · 5.5 KB
/
sudoku.h
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
#ifndef SUDOKU_H
#define SUDOKU_H
#include "rlib.h"
#include "rsolve.h"
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define ANSII_BLUE_BG "\033[34m"
#define ANSII_RED_BG "\033[31m"
#define ANSII_GREEN_BG "\033[32m"
#define ANSII_CLEAR "\033[0m"
#define N 9
void draw_cell(int c)
{
if (c > 0 && c <= 3)
{
printf("%s", ANSII_BLUE_BG);
}
else if (c > 0 && c <= 6)
{
printf("%s", ANSII_GREEN_BG);
}
else if (c > 0 && c <= 9)
{
printf("%s", ANSII_RED_BG);
}
if(c){
printf("%s%d ", c > 9 ? "" :" ", c );
}else{
printf(" 0 ");
}
printf("%s", ANSII_CLEAR);
}
char * grid_to_string(int grid[N][N]){
static char result[1024];
result[0] = 0;
for (int row = 0; row < N; row++) {
for (int col = 0; col < N; col++) {
char chunk[4];
chunk[0] = 0;
sprintf(chunk,"%d ",grid[row][col]);
strcat(result,chunk);
}
strcat(result,"\n");
}
return result;
}
void print_grid(int grid[N][N],bool clear) {
if(clear)
printf("\033[2J\033[H");
for (int row = 0; row < N; row++) {
for (int col = 0; col < N; col++) {
draw_cell(grid[row][col]);
///printf("%d ", grid[row][col]);
}
printf("\n");
}
}
int count_neighbors2(int grid[N][N],int row, int col){
// Check row
int num = -1;
int neighbors = 0;
for (int x = 0; x < N; x++) {
if(grid[row][x])
neighbors++;
if (grid[row][x] == num) {
return 0;
}
}
// Check column
for (int x = 0; x < N; x++) {
if(grid[x][col])
neighbors++;
if (grid[x][col] == num) {
return 0;
}
}
// Check box
int startRow = row - row % (N / 3), startCol = col - col % (N / 3);
for (int i = 0; i < N / 3; i++) {
for (int j = 0; j < N / 3; j++) {
if(grid[i + startRow][j + startCol])
neighbors++;
if (grid[i + startRow][j + startCol] == num) {
return 0;
}
}
}
return neighbors;
}
int is_safe(int grid[N][N], int row, int col, int num) {
//if(count_neighbors(grid, row,col) < 4)
// return false;
// Check row
for (int x = 0; x < N; x++) {
if (grid[row][x] == num) {
return false;
}
}
// Check column
for (int x = 0; x < N; x++) {
if (grid[x][col] == num) {
return false;
}
}
// Check box
int startRow = row - row % (N / 3), startCol = col - col % (N / 3);
for (int i = 0; i < N / 3; i++) {
for (int j = 0; j < N / 3; j++) {
if (grid[i + startRow][j + startCol] == num) {
return false;
}
}
}
return true;
}
void grid_reset(int * grid){
memset(grid,0,N*N*sizeof(int));
}
int * grid_copy(int * grid){
int * new_grid = malloc(N*N*sizeof(int));
memcpy(new_grid,grid,N*N*sizeof(int));
return new_grid;
}
int * grid_new(){
return (int *)calloc(sizeof(int),N*N);
}
bool empty_spot_is_available(int grid[N][N]){
for (unsigned int row = 0; row < N; row++) {
for (unsigned int col = 0; col < N; col++) {
if (grid[row][col] == 0) {
return true;
}
}
}
return false;
}
unsigned long long _solve(int grid[N][N], unsigned long long *attempts, bool draw){
(*attempts)++;
unsigned int row, col;
if(!get_easiest_cell(grid,&row,&col)){
//print_grid(grid, false);
return *attempts;
}
for(int num = 1; num < N + 1; num++){
if(is_safe(grid,row,col,num)){
grid[row][col] = num;
//print_grid(grid,true);
if(_solve(grid,attempts,draw))
{
return *attempts;
}
grid[row][col] = 0;
}
}
return 0;
}
unsigned int _solve2(int grid[N][N], unsigned long long * attempts, bool draw) {
(*attempts)++;
unsigned int row, col;
bool emptySpot = false;
for (row = 0; row < N; row++) {
for (col = 0; col < N; col++) {
if (grid[row][col] == 0) {
// if(count_neighbors(grid,row,col) == 8){
// print_grid(grid,true);
// printf("Found neighbors\n");
// exit(0);
//}
emptySpot = true;
break;
}
}
if (emptySpot) {
break;
}
}
if(!emptySpot)
return true;
/*
if (!empty_spot_is_available(grid)) {
return true;
}*/
for (int num = 1; num <= 9; num++) {
//unsigned int * easy_row = calloc(sizeof(int),1);
//unsigned int * easy_col = calloc(sizeof(int), 1);
//if(get_easiest_cell(grid,easy_row,easy_col)){
// row = *easy_row;
// free(easy_row);
// col = *easy_col;
// free(easy_col);
//}
if (is_safe(grid, row, col, num)) {
grid[row][col] = num;
if(draw)
print_grid(grid,true);
if (_solve2(grid,attempts,draw)) {
return *attempts;
}
grid[row][col] = 0;
}
}
return 0;
}
unsigned int solve2(int grid[N][N], bool draw){
unsigned long long attempts = 0;
return _solve(grid,&attempts, draw);
}
unsigned int solve(int grid[N][N],bool draw) {
unsigned long long attempts = 0;
return _solve2(grid,&attempts, draw);
}
#endif