-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC++.cpp
270 lines (229 loc) · 5.68 KB
/
C++.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
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
const int GRID_SIZE = 9;
const int SQUARE_SIZE = 3;
const int EMPTY = 0;
struct Point
{
int x, y;
};
class Sudoku
{
public:
int **grid;
vector<Point> history;
Sudoku(int **grid)
{
this->grid = grid;
}
void updateCase(int x, int y, int value)
{
this->grid[y][x] = value;
this->history.push_back({x, y});
}
void undo()
{
Point lastPlay = this->history.back();
this->grid[lastPlay.y][lastPlay.x] = EMPTY;
this->history.pop_back();
}
Point firstEmpty()
{
for (int y = 0; y < GRID_SIZE; y++)
{
for (int x = 0; x < GRID_SIZE; x++)
{
if (this->grid[y][x] == EMPTY)
{
return {x, y};
}
}
}
return {-1, -1};
}
bool checkRow(int index)
{
bool checked[GRID_SIZE] = {false, false, false, false, false, false, false, false, false};
for (int x = 0; x < GRID_SIZE; x++)
{
int number = this->grid[index][x];
if (number == EMPTY)
{
continue;
}
if (checked[number - 1])
{
return false;
}
checked[number - 1] = true;
}
return true;
}
bool checkCol(int index)
{
bool checked[GRID_SIZE] = {false, false, false, false, false, false, false, false, false};
for (int y = 0; y < GRID_SIZE; y++)
{
int number = this->grid[y][index];
if (number == EMPTY)
{
continue;
}
if (checked[number - 1])
{
return false;
}
checked[number - 1] = true;
}
return true;
}
bool checkSquare(int index)
{
bool checked[GRID_SIZE] = {false, false, false, false, false, false, false, false, false};
int rowOffset = index - (index % SQUARE_SIZE);
int colOffset = (index % SQUARE_SIZE) * SQUARE_SIZE;
for (int x = 0; x < SQUARE_SIZE; x++)
{
for (int y = 0; y < SQUARE_SIZE; y++)
{
int _x = x + colOffset;
int _y = y + rowOffset;
int number = this->grid[_y][_x];
if (number == EMPTY)
{
continue;
}
if (checked[number - 1])
{
return false;
}
checked[number - 1] = true;
}
}
return true;
}
bool isValid()
{
for (int i = 0; i < GRID_SIZE; i++)
{
if (!this->checkRow(i))
{
return false;
}
if (!this->checkCol(i))
{
return false;
}
if (!this->checkSquare(i))
{
return false;
}
}
return true;
}
bool isEnd()
{
Point pos = this->firstEmpty();
return pos.x == -1 && pos.y == -1;
}
vector<int> getPossibleNumber(int x, int y)
{
bool checked[GRID_SIZE] = {false, false, false, false, false, false, false, false, false};
for (int _x = 0; _x < GRID_SIZE; _x++)
{
int number = this->grid[y][_x];
if (number == EMPTY)
{
continue;
}
checked[number - 1] = true;
}
for (int _y = 0; _y < GRID_SIZE; _y++)
{
int number = this->grid[_y][x];
if (number == EMPTY)
{
continue;
}
checked[number - 1] = true;
}
int indexSquare = SQUARE_SIZE * floor(y / SQUARE_SIZE) + floor(x / SQUARE_SIZE);
int rowOffset = indexSquare - (indexSquare % SQUARE_SIZE);
int colOffset = (indexSquare % SQUARE_SIZE) * SQUARE_SIZE;
for (int _x = 0; _x < SQUARE_SIZE; _x++)
{
for (int _y = 0; _y < SQUARE_SIZE; _y++)
{
int __x = _x + colOffset;
int __y = _y + rowOffset;
int number = this->grid[__y][__x];
if (number == EMPTY)
{
continue;
}
checked[number - 1] = true;
}
}
vector<int> result;
for (int i = 0; i < GRID_SIZE; i++)
{
if (!checked[i])
{
result.push_back(i + 1);
}
}
return result;
}
};
bool solve(Sudoku sudoku)
{
if (sudoku.isEnd())
{
return sudoku.isValid();
}
Point pos = sudoku.firstEmpty();
vector<int> possibleNumber = sudoku.getPossibleNumber(pos.x, pos.y);
if (possibleNumber.empty())
{
sudoku.undo();
return false;
}
for (int n : possibleNumber)
{
sudoku.updateCase(pos.x, pos.y, n);
if (solve(sudoku))
{
return true;
}
}
sudoku.undo();
return false;
}
int main()
{
int **grid = new int *[GRID_SIZE];
for (int y = 0; y < GRID_SIZE; y++)
{
grid[y] = new int[GRID_SIZE];
string line;
getline(cin, line);
for (int x = 0; x < GRID_SIZE; x++)
{
grid[y][x] = line[x] - '0';
}
}
Sudoku s = {grid};
solve(s);
for (int y = 0; y < GRID_SIZE; y++)
{
for (int x = 0; x < GRID_SIZE; x++)
{
cout << s.grid[y][x];
}
cout << '\n';
}
}