-
Notifications
You must be signed in to change notification settings - Fork 1
/
23-valid-sudoku.cpp
55 lines (53 loc) · 1.48 KB
/
23-valid-sudoku.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
class Solution {
public:
bool isValidSudoku(vector<vector<char>>& mat) {
int n = mat.size();
for(int i = 0; i < n; ++i)
{
vector<bool> vec(10, false);
for(int j = 0; j < n; ++j)
{
if(mat[i][j] != '.')
{
if(vec[mat[i][j] - '0'])
return 0;
vec[mat[i][j] - '0'] = true;
}
}
}
for(int i = 0; i < n; ++i)
{
vector<bool> vec(10, false);
for(int j = 0; j < n; ++j)
{
if(mat[j][i] != '.')
{
if(vec[mat[j][i] - '0'])
return 0;
vec[mat[j][i] - '0'] = true;
}
}
}
// cout << "here\n";
for(int i = 0; i < n; i += 3)
{
for(int j = 0; j < n; j += 3)
{
vector<bool> vec(10, false);
for(int k = i; k < (3 + i); ++k)
{
for(int x = j; x < (3 + j); ++x)
{
if(mat[k][x] != '.')
{
if(vec[mat[k][x] - '0'])
return 0;
vec[mat[k][x] - '0'] = true;
}
}
}
}
}
return true;
}
};