Skip to content

Commit 2a7196d

Browse files
committed
"Sudoku Solver"
1 parent 741fc6c commit 2a7196d

File tree

2 files changed

+164
-1
lines changed

2 files changed

+164
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ The `☢` means that you need to have a LeetCode Premium Subscription.
254254
| 40 | [Combination Sum II] | [C++](src/40.cpp) |
255255
| 39 | [Combination Sum] | [C++](src/39.cpp) |
256256
| 38 | [Count and Say] | [C](src/38.c) |
257-
| 37 | [Sudoku Solver] | |
257+
| 37 | [Sudoku Solver] | [C](src/37.cpp) |
258258
| 36 | [Valid Sudoku] | [C](src/36.c) |
259259
| 35 | [Search Insert Position] | [C](src/35.c) |
260260
| 34 | [Search for a Range] | [C++](src/34.cpp) |

src/37.cpp

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <string>
4+
5+
using namespace std;
6+
7+
class Solution {
8+
public:
9+
struct Trace {
10+
int i;
11+
int j;
12+
int s;
13+
Trace() : i(0), j(0), s(0) {}
14+
Trace(int _i, int _j, int _s) : i(_i), j(_j), s(_s) {}
15+
};
16+
17+
void solveSudoku(vector<vector<char>>& board) {
18+
vector<vector<bool>> rowFlag(9, vector<bool>(9, false));
19+
vector<vector<bool>> colFlag(9, vector<bool>(9, false));
20+
vector<vector<bool>> boxFlag(9, vector<bool>(9, false));
21+
vector<vector<bool>> occupied(9, vector<bool>(9, false));
22+
int i, j;
23+
int remains = 0;
24+
for (i = 0; i < 9; i++) {
25+
for (j = 0; j < 9; j++) {
26+
if (board[i][j] != '.') {
27+
int k = board[i][j] - '1';
28+
rowFlag[i][k] = true;
29+
colFlag[j][k] = true;
30+
boxFlag[(i / 3) * 3 + j / 3][k] = true;
31+
occupied[i][j] = true;
32+
}
33+
else {
34+
remains++;
35+
}
36+
}
37+
}
38+
39+
bool found = false;
40+
vector<Trace> ans(remains);
41+
placeNumber(board, found, ans, 0, remains, occupied, rowFlag, colFlag, boxFlag);
42+
}
43+
44+
void placeNumber(vector<vector<char>>& board, bool &found,
45+
vector<Trace> &ans, int count, int remains,
46+
vector<vector<bool>> &occupied, vector<vector<bool>> &rowFlag,
47+
vector<vector<bool>> &colFlag, vector<vector<bool>> &boxFlag)
48+
{
49+
// suppose we have only one solution, if we found one, no need to continue
50+
if (found) return;
51+
52+
int i, j;
53+
54+
// found one solution! copy answers to the board
55+
if (count == remains) {
56+
found = true;
57+
int k = 0;
58+
for (i = 0; i < 9; i++) {
59+
for (j = 0; j < 9; j++) {
60+
if (board[i][j] == '.') {
61+
board[i][j] = ans[k++].s + '1';
62+
}
63+
}
64+
}
65+
return;
66+
}
67+
68+
for (i = 0; i < 9; i++) {
69+
for (j = 0; j < 9; j++) {
70+
if (occupied[i][j] == false) {
71+
for (int s = 0; s < 9; s++) {
72+
// try to place a number
73+
ans[count] = Trace(i, j, s);
74+
// if it's valid, set flag and place another number
75+
if (!rowFlag[i][s] && !colFlag[j][s] && !boxFlag[(i / 3) * 3 + j / 3][s]) {
76+
rowFlag[i][s] = true;
77+
colFlag[j][s] = true;
78+
boxFlag[(i / 3) * 3 + j / 3][s] = true;
79+
occupied[i][j] = true;
80+
placeNumber(board, found, ans, count + 1, remains, occupied, rowFlag, colFlag, boxFlag);
81+
}
82+
}
83+
// can't find a right number, backtrack
84+
// set back the flag to false
85+
// be aware to set the previout number, not the current
86+
if (count <= 0) return;
87+
Trace t = ans[count - 1];
88+
rowFlag[t.i][t.s] = false;
89+
colFlag[t.j][t.s] = false;
90+
boxFlag[(t.i / 3) * 3 + t.j / 3][t.s] = false;
91+
occupied[t.i][t.j] = false;
92+
return;
93+
}
94+
}
95+
}
96+
}
97+
};
98+
99+
int main() {
100+
vector<string> board0 = {
101+
"..9748...",
102+
"7........",
103+
".2.1.9...",
104+
"..7...24.",
105+
".64.1.59.",
106+
".98...3..",
107+
"...8.3.2.",
108+
"........6",
109+
"...2759.."
110+
};
111+
112+
vector<string> board1 = {
113+
"53..7....",
114+
"6..195...",
115+
".98....6.",
116+
"8...6...3",
117+
"4..8.3..1",
118+
"7...2...6",
119+
".6....28.",
120+
"...419..5",
121+
"....8..79"
122+
};
123+
124+
vector<vector<char> > board(9, vector<char>(9, 0));
125+
int i, j;
126+
127+
// board0
128+
for (i = 0; i < board0.size(); i++) {
129+
for (j = 0; j < board0[i].length(); j++) {
130+
board[i][j] = board0[i][j];
131+
}
132+
}
133+
134+
Solution s;
135+
s.solveSudoku(board);
136+
137+
for (i = 0; i < board.size(); i++) {
138+
for (j = 0; j < board[i].size(); j++) {
139+
printf("%c ", board[i][j]);
140+
}
141+
printf("\n");
142+
}
143+
144+
printf("-----------------\n");
145+
146+
// board1
147+
for (i = 0; i < board1.size(); i++) {
148+
for (j = 0; j < board1[i].length(); j++) {
149+
board[i][j] = board1[i][j];
150+
}
151+
}
152+
153+
s.solveSudoku(board);
154+
155+
for (i = 0; i < board.size(); i++) {
156+
for (j = 0; j < board[i].size(); j++) {
157+
printf("%c ", board[i][j]);
158+
}
159+
printf("\n");
160+
}
161+
162+
return 0;
163+
}

0 commit comments

Comments
 (0)