forked from lennylxx/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
37.cpp
229 lines (204 loc) · 6.94 KB
/
37.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
#include <iostream>
#include <vector>
#include <string>
using namespace std;
// 108 ms
class Solution0 {
public:
struct Trace {
int i;
int j;
int s;
Trace() : i(0), j(0), s(0) {}
Trace(int _i, int _j, int _s) : i(_i), j(_j), s(_s) {}
};
void solveSudoku(vector<vector<char>>& board) {
vector<vector<bool>> rowFlag(9, vector<bool>(9, false));
vector<vector<bool>> colFlag(9, vector<bool>(9, false));
vector<vector<bool>> boxFlag(9, vector<bool>(9, false));
vector<vector<bool>> occupied(9, vector<bool>(9, false));
int i, j;
int remains = 0;
for (i = 0; i < 9; i++) {
for (j = 0; j < 9; j++) {
if (board[i][j] != '.') {
int k = board[i][j] - '1';
rowFlag[i][k] = true;
colFlag[j][k] = true;
boxFlag[(i / 3) * 3 + j / 3][k] = true;
occupied[i][j] = true;
}
else {
remains++;
}
}
}
bool found = false;
vector<Trace> ans(remains);
placeNumber(board, found, ans, 0, remains, occupied, rowFlag, colFlag, boxFlag);
}
void placeNumber(vector<vector<char>>& board, bool &found,
vector<Trace> &ans, int count, int remains,
vector<vector<bool>> &occupied, vector<vector<bool>> &rowFlag,
vector<vector<bool>> &colFlag, vector<vector<bool>> &boxFlag)
{
// suppose we have only one solution, if we found one, no need to continue
if (found) return;
int i, j;
// found one solution! copy answers to the board
if (count == remains) {
found = true;
int k = 0;
for (i = 0; i < 9; i++) {
for (j = 0; j < 9; j++) {
if (board[i][j] == '.') {
board[i][j] = ans[k++].s + '1';
}
}
}
return;
}
for (i = 0; i < 9; i++) {
for (j = 0; j < 9; j++) {
if (occupied[i][j] == false) {
for (int s = 0; s < 9; s++) {
// try to place a number
ans[count] = Trace(i, j, s);
// if it's valid, set flag and place another number
if (!rowFlag[i][s] && !colFlag[j][s] && !boxFlag[(i / 3) * 3 + j / 3][s]) {
rowFlag[i][s] = true;
colFlag[j][s] = true;
boxFlag[(i / 3) * 3 + j / 3][s] = true;
occupied[i][j] = true;
placeNumber(board, found, ans, count + 1, remains, occupied, rowFlag, colFlag, boxFlag);
}
}
// can't find a right number, backtrack
// set back the flag to false
// be aware to set the previous number, not the current
if (count <= 0) return;
Trace t = ans[count - 1];
rowFlag[t.i][t.s] = false;
colFlag[t.j][t.s] = false;
boxFlag[(t.i / 3) * 3 + t.j / 3][t.s] = false;
occupied[t.i][t.j] = false;
return;
}
}
}
}
};
// 36 ms
class Solution {
public:
void solveSudoku(vector<vector<char>>& board) {
vector<vector<bool>> rowFlag(10, vector<bool>(10, false));
vector<vector<bool>> colFlag(10, vector<bool>(10, false));
vector<vector<bool>> boxFlag(10, vector<bool>(10, false));
int i, j;
for (i = 0; i < 9; i++) {
for (j = 0; j < 9; j++) {
if (board[i][j] != '.') {
int k = board[i][j] - '1';
rowFlag[i][k] = true;
colFlag[j][k] = true;
boxFlag[(i / 3) * 3 + j / 3][k] = true;
}
}
}
bool found = false;
placeNumber(board, found, 0, rowFlag, colFlag, boxFlag);
}
bool placeNumber(vector<vector<char>>& board, bool &found,
int depth,
vector<vector<bool>> &rowFlag,
vector<vector<bool>> &colFlag, vector<vector<bool>> &boxFlag)
{
// suppose we have only one solution, if we found one, no need to continue
if (found) return true;
// found one solution! copy answers to the board
if (depth == 81) {
found = true;
return true;
}
int i, j;
i = depth / 9; j = depth % 9;
if (board[i][j] != '.') {
return placeNumber(board, found, depth + 1, rowFlag, colFlag, boxFlag);
}
else {
for (int s = 0; s < 9; s++) {
// if it's valid, set flag and place another number
if (!rowFlag[i][s] && !colFlag[j][s] && !boxFlag[(i / 3) * 3 + j / 3][s]) {
// try to place a number
board[i][j] = s + '1';
rowFlag[i][s] = true;
colFlag[j][s] = true;
boxFlag[(i / 3) * 3 + j / 3][s] = true;
if (placeNumber(board, found, depth + 1, rowFlag, colFlag, boxFlag)) return true;
// backtrack
rowFlag[i][s] = false;
colFlag[j][s] = false;
boxFlag[(i / 3) * 3 + j / 3][s] = false;
board[i][j] = '.';
}
}
return false;
}
}
};
int main() {
vector<string> board0 = {
"..9748...",
"7........",
".2.1.9...",
"..7...24.",
".64.1.59.",
".98...3..",
"...8.3.2.",
"........6",
"...2759.."
};
vector<string> board1 = {
"53..7....",
"6..195...",
".98....6.",
"8...6...3",
"4..8.3..1",
"7...2...6",
".6....28.",
"...419..5",
"....8..79"
};
vector<vector<char> > board(9, vector<char>(9, 0));
int i, j;
// board0
for (i = 0; i < board0.size(); i++) {
for (j = 0; j < board0[i].length(); j++) {
board[i][j] = board0[i][j];
}
}
Solution s;
s.solveSudoku(board);
for (i = 0; i < board.size(); i++) {
for (j = 0; j < board[i].size(); j++) {
printf("%c ", board[i][j]);
}
printf("\n");
}
printf("-----------------\n");
// board1
for (i = 0; i < board1.size(); i++) {
for (j = 0; j < board1[i].length(); j++) {
board[i][j] = board1[i][j];
}
}
s.solveSudoku(board);
for (i = 0; i < board.size(); i++) {
for (j = 0; j < board[i].size(); j++) {
printf("%c ", board[i][j]);
}
printf("\n");
}
return 0;
}