-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cc
238 lines (192 loc) · 6.08 KB
/
main.cc
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
#include "raylib.h"
#include <iostream>
#include <stack>
#include <string>
#include <vector>
#define N 9
#define W 9
#define H 9
#define GRID_FULL (-1)
int idx(int r, int c);
int get_next_empty_cell(const std::vector<int> &g);
bool is_valid(const std::vector<int> &puzzle, int p, int val);
bool is_puzzle_valid(const std::vector<int> &puzzle);
bool in_3x3(const std::vector<int> &puzzle, int r, int c, int val);
bool in_row(const std::vector<int> &puzzle, int r, int val);
bool in_col(const std::vector<int> &puzzle, int c, int val);
void draw_board(const std::vector<int> &v);
struct State {
int pos;
int val;
State(int _pos, int _val) : pos{_pos}, val{_val} {}
};
constexpr float w = 50;
bool solved = false;
std::stack<State> stack;
bool original_grid[N * N];
int main(void) {
const int screenWidth = int(w * N);
const int screenHeight = int(w * N);
auto grid = std::vector<int>{
5, 3, 0, 0, 7, 0, 0, 0, 0,
6, 0, 0, 1, 9, 5, 0, 0, 0,
0, 9, 8, 0, 0, 0, 0, 6, 0,
8, 0, 0, 0, 6, 0, 0, 0, 3,
4, 0, 0, 8, 0, 3, 0, 0, 1,
7, 0, 0, 0, 2, 0, 0, 0, 6,
0, 6, 0, 0, 0, 0, 2, 8, 0,
0, 0, 0, 4, 1, 9, 0, 0, 5,
0, 0, 0, 0, 8, 0, 0, 7, 9,
};
if (!is_puzzle_valid(grid)) {
std::cerr << "[Error] Puzzle is not valid\n";
return 1;
}
InitWindow(screenWidth, screenHeight, "Recursive Sudoku Solver Visualizer");
// SetTargetFPS(60);
// It's a mark for the initially given values
// to draw the fixed grid
for (int i = 0; i < N * N; ++i) {
original_grid[i] = grid[i] != 0;
}
// assume the first grid accept 1.
// and push it to the stack.
int pos = get_next_empty_cell(grid);
stack.push(State(pos, 1));
while (!WindowShouldClose()) {
// if it is not solved yet
if (!solved) {
// we have state to deal with
if (!stack.empty()) {
State curr = stack.top();
int position = curr.pos;
int val = curr.val;
if (is_valid(grid, position, val)) {
grid[position] = val;
int next_empty = get_next_empty_cell(grid);
if (next_empty == GRID_FULL) {
solved = true;
}
stack.push(State(next_empty, 1));
} else {
// current cell is not valid
// check if the value is 9
// if it is we can't increment it further
// because sudoku accepts value [1, 9]
State prev = stack.top();
stack.pop();
if (prev.val == 9) {
grid[prev.pos] = 0;
} else {
prev.val++;
stack.push(prev);
}
}
}
}
BeginDrawing();
ClearBackground(BLACK);
draw_board(grid);
EndDrawing();
}
// TakeScreenshot("screenshot.png");
CloseWindow();
return 0;
}
int idx(int r, int c) { return r * N + c; }
int get_next_empty_cell(const std::vector<int> &g) {
for (int i = 0; i < N * N; ++i) {
if (g[i] == 0)
return i; // got an index
}
return GRID_FULL;
}
// Check if the value 'val' is in r-th row
bool in_row(const std::vector<int> &puzzle, int r, int val) {
for (int i = 0; i < N; ++i) {
if (puzzle[idx(r, i)] == val)
return true;
}
return false;
}
// Check if the value 'val' is in c-th column
bool in_col(const std::vector<int> &puzzle, int c, int val) {
for (int i = 0; i < N; ++i) {
if (puzzle[idx(i, c)] == val)
return true;
}
return false;
}
// Check if the value 'val' is in 3x3 box
bool in_3x3(const std::vector<int> &puzzle, int r, int c, int val) {
for (int i = r; i < r + 3; ++i) {
for (int j = c; j < c + 3; ++j) {
if (puzzle[idx(i, j)] == val)
return true;
}
}
return false;
}
// Check if p position is a valid position
bool is_valid(const std::vector<int> &puzzle, int p, int val) {
// Trick to map 1D index to 2D index
int c = p % N;
int r = p / N;
return !in_row(puzzle, r, val) && !in_col(puzzle, c, val) &&
!in_3x3(puzzle, r - r % 3, c - c % 3, val) && val >= 0 && val <= 9;
}
bool is_puzzle_valid(const std::vector<int> &puzzle) {
for (int pos = 0; pos < static_cast<int>(puzzle.size()); ++pos) {
int val = puzzle[pos];
if (val != 0) {
int c = pos % N;
int r = pos / N;
int row_count = 0;
for (int i = 0; i < N; ++i) {
if (puzzle[idx(r, i)] == val)
row_count++;
}
int col_count = 0;
for (int i = 0; i < N; ++i) {
if (puzzle[idx(i, c)] == val)
col_count++;
}
int box_count = 0;
int corner_row = r - r % 3;
int corner_col = c - c % 3;
for (int i = corner_row; i < corner_row + 3; ++i) {
for (int j = corner_col; j < corner_col + 3; ++j) {
if (puzzle[idx(i, j)] == val)
box_count++;
}
}
if (row_count != 1 || col_count != 1 || box_count != 1) {
return false;
}
}
}
return true;
}
void draw_board(const std::vector<int> &grid) {
for (int j = 0; j < N; ++j) {
for (int i = 0; i < N; ++i) {
auto rec = Rectangle{
i * w, // x
j * w, // y
w, // width
w // height
};
if (grid[idx(j, i)] != 0) {
if (original_grid[idx(j, i)]) {
DrawRectangleRec(rec, DARKPURPLE);
}
auto str = std::to_string(grid[idx(j, i)]);
auto font_size = 20;
int str_size = MeasureText(str.c_str(), font_size);
DrawText(str.c_str(), i * w + w / 2.0 - str_size / 2.0,
j * w + w / 2 - font_size / 2, font_size, RAYWHITE);
}
DrawRectangleLinesEx(rec, 2, BLUE);
}
}
}