-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathsolve.cpp
81 lines (81 loc) · 1.7 KB
/
solve.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
#include <vector>
#include <iostream>
#include <cassert>
using namespace std;
class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
for (int i = 0; i < 9; ++i) {
if (!(check_row(board, i)
&& (check_col(board, i))
&& (check_box(board, i)))
)
return false;
}
return true;
}
private:
bool check_row(const vector<vector<char>> &a, int row) {
vector<bool> used(9, false);
for (char i : a[row]) {
if ('.' == i)
continue;
int pos = i - '0' - 1;
if (used[pos])
return false;
else
used[pos] = true;
}
return true;
}
bool check_col(const vector<vector<char>> &a, int col) {
vector<bool> used(9, false);
for (int i = 0; i < 9; ++i) {
if (a[i][col] == '.')
continue;
int pos = a[i][col] - '0' - 1;
if (used[pos])
return false;
else
used[pos] = true;
}
return true;
}
bool check_box(const vector<vector<char>> &a, int box) {
int row_begin = box / 3 * 3;
int col_begin = box % 3 * 3;
vector<bool> used(9, false);
for (int i = row_begin; i < row_begin + 3; ++i) {
for (int j = col_begin; j < col_begin + 3; ++j) {
if (a[i][j] == '.')
continue;
int pos = a[i][j] - '0' - 1;
if (used[pos])
return false;
else
used[pos] = true;
}
}
return true;
}
};
static void input(vector<vector<char>> &board, string s) {
vector<char> t;
assert(s.size() == 9);
for (auto c : s) {
t.push_back(c);
}
board.push_back(t);
}
int main(int arch, char **argv)
{
Solution solution;
vector<vector<char>> board;
for (int i = 0; i < 9; ++i) {
string a;
cin >> a;
input(board, a);
}
cout << solution.isValidSudoku(board) << endl;
return 0;
}