Skip to content

Commit 880606e

Browse files
committed
36 Valid Sudoku
1 parent db05338 commit 880606e

File tree

4 files changed

+183
-0
lines changed

4 files changed

+183
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
+ [33 Search in Rotated Sorted Array(二分搜索)](algorithms/SearchinRotatedSortedArray)
2929
+ [34 Search for a Range(二分)](algorithms/SearchforaRange)
3030
+ [35 Search Insert Position(二分查找)](algorithms/SearchInsertPosition)
31+
+ [36 Valid Sudoku(数独、hash表)](algorithms/ValidSudoku)
3132
+ [38 Count and Say(字符串和数字转化详解)](algorithms/CountandSay)
3233
+ [43 Multiply Strings(大数乘法)](algorithms/MultiplyStrings)
3334
+ [46 Permutations(全排列)](algorithms/Permutations)

algorithms/ValidSudoku/README.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
## Valid Sudoku
2+
3+
Determine if a Sudoku is valid, according to: [Sudoku Puzzles - The Rules](http://sudoku.com.au/TheRules.aspx).
4+
5+
The Sudoku board could be partially filled, where empty cells are filled with the character '.'.
6+
7+
8+
A partially filled sudoku which is valid.
9+
10+
Note:
11+
*A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.
12+
13+
Subscribe to see which companies asked this question*
14+
15+
## Solution
16+
17+
此题是纯粹的模拟题,只需要根据数独的规则分别判断每一行、每一列、每一个九宫格是否出现重复数字即可。判断数字是否重复使用hash表即可。
18+
19+
### 判断每一行
20+
21+
```cpp
22+
bool check_row(const vector<vector<char>> &a, int row) {
23+
vector<bool> used(9, false);
24+
for (char i : a[row]) {
25+
if ('.' == i)
26+
continue;
27+
int pos = i - '0' - 1;
28+
if (used[pos])
29+
return false;
30+
else
31+
used[pos] = true;
32+
}
33+
return true;
34+
}
35+
```
36+
37+
### 判断每一列
38+
39+
```cpp
40+
bool check_col(const vector<vector<char>> &a, int col) {
41+
vector<bool> used(9, false);
42+
for (int i = 0; i < 9; ++i) {
43+
if (a[i][col] == '.')
44+
continue;
45+
int pos = a[i][col] - '0' - 1;
46+
if (used[pos])
47+
return false;
48+
else
49+
used[pos] = true;
50+
}
51+
return true;
52+
}
53+
```
54+
55+
### 判断每一个九宫格
56+
57+
需要注意确定每一个九宫格对应行首和列首,假设九宫格从0开始编号,则第n个九宫格的行位置为`n / 3 * 3`, 列位置为`n % 3 * 3`
58+
59+
```cpp
60+
bool check_box(const vector<vector<char>> &a, int box) {
61+
int row_begin = box / 3 * 3;
62+
int col_begin = box % 3 * 3;
63+
vector<bool> used(9, false);
64+
for (int i = row_begin; i < row_begin + 3; ++i) {
65+
for (int j = col_begin; j < col_begin + 3; ++j) {
66+
if (a[i][j] == '.')
67+
continue;
68+
int pos = a[i][j] - '0' - 1;
69+
if (used[pos])
70+
return false;
71+
else
72+
used[pos] = true;
73+
}
74+
}
75+
return true;
76+
}
77+
```
78+
79+
实习代码为:
80+
81+
```cpp
82+
bool isValidSudoku(vector<vector<char>>& board) {
83+
for (int i = 0; i < 9; ++i) {
84+
if (!(check_row(board, i)
85+
&& (check_col(board, i))
86+
&& (check_box(board, i)))
87+
)
88+
return false;
89+
}
90+
return true;
91+
}
92+
```

algorithms/ValidSudoku/in.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.87654321
2+
2........
3+
2........
4+
4........
5+
5........
6+
6........
7+
7........
8+
8........
9+
9........

algorithms/ValidSudoku/solve.cpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#include <vector>
2+
#include <iostream>
3+
#include <cassert>
4+
using namespace std;
5+
class Solution {
6+
public:
7+
bool isValidSudoku(vector<vector<char>>& board) {
8+
for (int i = 0; i < 9; ++i) {
9+
if (!(check_row(board, i)
10+
&& (check_col(board, i))
11+
&& (check_box(board, i)))
12+
)
13+
return false;
14+
}
15+
return true;
16+
}
17+
private:
18+
bool check_row(const vector<vector<char>> &a, int row) {
19+
vector<bool> used(9, false);
20+
for (char i : a[row]) {
21+
if ('.' == i)
22+
continue;
23+
int pos = i - '0' - 1;
24+
if (used[pos])
25+
return false;
26+
else
27+
used[pos] = true;
28+
}
29+
return true;
30+
}
31+
bool check_col(const vector<vector<char>> &a, int col) {
32+
vector<bool> used(9, false);
33+
for (int i = 0; i < 9; ++i) {
34+
if (a[i][col] == '.')
35+
continue;
36+
int pos = a[i][col] - '0' - 1;
37+
if (used[pos])
38+
return false;
39+
else
40+
used[pos] = true;
41+
}
42+
return true;
43+
}
44+
bool check_box(const vector<vector<char>> &a, int box) {
45+
int row_begin = box / 3 * 3;
46+
int col_begin = box % 3 * 3;
47+
vector<bool> used(9, false);
48+
for (int i = row_begin; i < row_begin + 3; ++i) {
49+
for (int j = col_begin; j < col_begin + 3; ++j) {
50+
if (a[i][j] == '.')
51+
continue;
52+
int pos = a[i][j] - '0' - 1;
53+
if (used[pos])
54+
return false;
55+
else
56+
used[pos] = true;
57+
}
58+
}
59+
return true;
60+
}
61+
};
62+
static void input(vector<vector<char>> &board, string s) {
63+
vector<char> t;
64+
assert(s.size() == 9);
65+
for (auto c : s) {
66+
t.push_back(c);
67+
}
68+
board.push_back(t);
69+
}
70+
int main(int arch, char **argv)
71+
{
72+
Solution solution;
73+
vector<vector<char>> board;
74+
for (int i = 0; i < 9; ++i) {
75+
string a;
76+
cin >> a;
77+
input(board, a);
78+
}
79+
cout << solution.isValidSudoku(board) << endl;
80+
return 0;
81+
}

0 commit comments

Comments
 (0)