-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path36.cpp
181 lines (170 loc) · 5.9 KB
/
36.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
__________________________________________________________________________________________________
8ms
static const int __ = []() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
return 0;
}();
class Solution {
public:
bool isValidSudoku(vector<vector<char>>& b) {
char cMap[9] = {0}, i, j, p, q, r;
for (i = 0; i < 9; i++) {
memset(cMap, 0, 9);
for (j = 0; j < 9; j++) if (b[i][j] > 46) cMap[b[i][j] - 49]++;
j = 0;
while (j < 9 && cMap[j] <= 1) j++;
if (j != 9) return false;
}
for (i = 0; i < 9; i++) {
memset(cMap, 0, 9);
for (j = 0; j < 9; j++) if (b[j][i] > 46) cMap[b[j][i] - 49]++;
j = 0;
while (j < 9 && cMap[j] <= 1) j++;
if (j != 9) return false;
}
for (p = 3; p <= 9; p += 3) {
for (q = 3; q <= 9; q += 3) {
memset(cMap, 0, 9);
for (i = p - 3; i < p; i++) {
for (j = q - 3; j < q; j++) if (b[i][j] > 46) cMap[b[i][j] - 49]++;
j = 0;
while (j < 9 && cMap[j] <= 1) j++;
if (j != 9) return false;
}
}
}
return true;
}
};
__________________________________________________________________________________________________
12ms
class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
int hor[9][9] = {0}, ver[9][9] = {0}, sqr[9][9] = {0};
for(int i = 0; i < 9; i++) {
for(int j = 0; j < 9; j++) {
char c = board[i][j];
if(c != '.') {
if(hor[i][c % 9]++) return false;
if(ver[j][c % 9]++) return false;
if(sqr[(i / 3) * 3 + j / 3][c % 9]++) return false;
}
}
}
return true;
}
};
__________________________________________________________________________________________________
16ms
static const int __ = []() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
return 0;
}();
class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
int* row[9];
int* col[9];
int* box[9];
for(int i = 0; i < 9; i++){
row[i] = new int[9]{0};
col[i] = new int[9]{0};
box[i] = new int[9]{0};
}
int bn = 0;
for(int i = 0; i < 9; i += 3){
for(int j = 0; j < 9; j += 3){
int rl = i+3;
int cl = j+3;
for(int m = i; m < rl; m++){
for(int n = j; n < cl; n++){
if(board[m][n] != '.'){
int val = board[m][n] - '0' - 1;
if(row[m][val] == 1)return false;
row[m][val] = 1;
if(col[n][val] == 1)return false;
col[n][val] = 1;
if(box[bn][val] == 1)return false;
box[bn][val] = 1;
}
}
}
bn++;
}
}
return true;
}
};
__________________________________________________________________________________________________
9352 kb
auto fast_io =[](){std::ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);return 0;}();
class Solution
{
public:
bool isValidSudoku(vector<vector<char>> &board)
{
int row[9][9] = {0}, col[9][9] = {0}, squ[9][9] = {0};
for (int i = 0; i < board.size(); ++i)
{
for (int j = 0; j < board[i].size(); ++j)
{
if (board[i][j] != '.')
{
int num = board[i][j] - '0' - 1, k = i /3* 3 + j / 3;
if (row[i][num] || col[j][num] || squ[k][num]) return false;
row[i][num] = col[j][num] = squ[k][num] = 1;
}
}
}
return true;
}
};
__________________________________________________________________________________________________
9376 kb
auto fast_io =[](){std::ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);return 0;}();
class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
const int N = board.size();
std::array<bool, 10> lineSeen { false };
std::array<bool, 10> colSeen { false };
std::array<bool, 10> blockSeen { false };
for (int i = 0; i < N; ++i)
{
for (int j = 0; j < N; ++j)
{
const int lineBlock = j / 3 + 3 * (i/3);
const int colBlock = j % 3 + 3 * (i%3);
const int digitLine = board[i][j] - '0';
const int digitCol = board[j][i] - '0';
const int digitBlock = board[lineBlock][colBlock] - '0';
if (digitLine > 0)
{
if (lineSeen[digitLine]) return false;
lineSeen[digitLine] = true;
}
if (digitCol > 0)
{
if (colSeen[digitCol]) return false;
colSeen[digitCol] = true;
}
// block number i, case number j
if (digitBlock > 0)
{
if (blockSeen[digitBlock]) return false;
blockSeen[digitBlock] = true;
}
}
std::fill_n(lineSeen.begin(), 10, false);
std::fill_n(colSeen.begin(), 10, false);
std::fill_n(blockSeen.begin(), 10, false);
}
return true;
}
};
__________________________________________________________________________________________________