Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fbb8e06

Browse files
committed
"Sudoku Solver": faster solution
1 parent e8a48a7 commit fbb8e06

File tree

1 file changed

+68
-2
lines changed

1 file changed

+68
-2
lines changed

‎src/37.cpp

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
using namespace std;
66

7-
class Solution {
7+
// 108 ms
8+
class Solution0 {
89
public:
910
struct Trace {
1011
int i;
@@ -82,7 +83,7 @@ class Solution {
8283
}
8384
// can't find a right number, backtrack
8485
// set back the flag to false
85-
// be aware to set the previout number, not the current
86+
// be aware to set the previous number, not the current
8687
if (count <= 0) return;
8788
Trace t = ans[count - 1];
8889
rowFlag[t.i][t.s] = false;
@@ -96,6 +97,71 @@ class Solution {
9697
}
9798
};
9899

100+
// 36 ms
101+
class Solution {
102+
public:
103+
void solveSudoku(vector<vector<char>>& board) {
104+
vector<vector<bool>> rowFlag(10, vector<bool>(10, false));
105+
vector<vector<bool>> colFlag(10, vector<bool>(10, false));
106+
vector<vector<bool>> boxFlag(10, vector<bool>(10, false));
107+
int i, j;
108+
for (i = 0; i < 9; i++) {
109+
for (j = 0; j < 9; j++) {
110+
if (board[i][j] != '.') {
111+
int k = board[i][j] - '1';
112+
rowFlag[i][k] = true;
113+
colFlag[j][k] = true;
114+
boxFlag[(i / 3) * 3 + j / 3][k] = true;
115+
}
116+
}
117+
}
118+
119+
bool found = false;
120+
placeNumber(board, found, 0, rowFlag, colFlag, boxFlag);
121+
}
122+
123+
bool placeNumber(vector<vector<char>>& board, bool &found,
124+
int depth,
125+
vector<vector<bool>> &rowFlag,
126+
vector<vector<bool>> &colFlag, vector<vector<bool>> &boxFlag)
127+
{
128+
// suppose we have only one solution, if we found one, no need to continue
129+
if (found) return true;
130+
131+
// found one solution! copy answers to the board
132+
if (depth == 81) {
133+
found = true;
134+
return true;
135+
}
136+
137+
int i, j;
138+
i = depth / 9; j = depth % 9;
139+
140+
if (board[i][j] != '.') {
141+
return placeNumber(board, found, depth + 1, rowFlag, colFlag, boxFlag);
142+
}
143+
else {
144+
for (int s = 0; s < 9; s++) {
145+
// if it's valid, set flag and place another number
146+
if (!rowFlag[i][s] && !colFlag[j][s] && !boxFlag[(i / 3) * 3 + j / 3][s]) {
147+
// try to place a number
148+
board[i][j] = s + '1';
149+
rowFlag[i][s] = true;
150+
colFlag[j][s] = true;
151+
boxFlag[(i / 3) * 3 + j / 3][s] = true;
152+
if (placeNumber(board, found, depth + 1, rowFlag, colFlag, boxFlag)) return true;
153+
// backtrack
154+
rowFlag[i][s] = false;
155+
colFlag[j][s] = false;
156+
boxFlag[(i / 3) * 3 + j / 3][s] = false;
157+
board[i][j] = '.';
158+
}
159+
}
160+
return false;
161+
}
162+
}
163+
};
164+
99165
int main() {
100166
vector<string> board0 = {
101167
"..9748...",

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /