-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsudoku2.cpp
75 lines (69 loc) · 2.26 KB
/
sudoku2.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
bool sudoku2(std::vector<std::vector<char>> grid) {
int n = grid[0].size();
int dim = 3;
int maxGrids = n/dim;
/* check single lines first */
for ( int i = 0; i < n; i++ ) {
std::map<int,int> digits;
for ( int j = 0; j < n; j++ ) {
if ( grid[i][j] != '.') {
int val = grid[i][j] - '0';
std::map<int,int>::iterator it;
it = digits.find(val);
if ( it != digits.end() ) {
std::cout << "i: " << i << "; j: " << j << std::endl;
return false;
}
digits.insert(std::pair<int,int>(val,1));
}
}
}
/* check single columns */
for ( int i = 0; i < n; i++ ) {
std::map<int,int> digits;
for ( int j = 0; j < n; j++ ) {
if ( grid[j][i] != '.') {
int val = grid[j][i] - '0';
std::map<int,int>::iterator it;
it = digits.find(val);
if ( it != digits.end() ) {
std::cout << "i: " << i << "; j: " << j << std::endl;
return false;
}
digits.insert(std::pair<int,int>(val,1));
}
}
}
int minRow = 0;
int maxRow = dim;
int shift = 0;
int count = 0;
while (shift < 9) {
count = 0;
int minCol = 0;
int maxCol = dim;
minRow = shift;
maxRow = shift + dim;
while (count < 3) {
std::map<int,int> digits;
for ( int i = minRow; i < maxRow; i++ ) {
for ( int j = minCol; j < maxCol; j++ ) {
if ( grid[i][j] != '.') {
int val = grid[i][j] - '0';
std::map<int,int>::iterator it;
it = digits.find(val);
if ( it != digits.end() ) {
return false;
}
digits.insert(std::pair<int,int>(val,1));
}
}
}
minCol += dim;
maxCol += dim;
count++;
}
shift += dim;
}
return true;
}