-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab_6.cpp
More file actions
63 lines (54 loc) · 1.49 KB
/
Copy pathLab_6.cpp
File metadata and controls
63 lines (54 loc) · 1.49 KB
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
#include <iostream>
#include <vector>
#include <set>
using namespace std;
bool isValidSudoku(vector<vector<char>>& board, int n) {
for (int i = 0; i < n; i++) {
set<char> row;
for (int j = 0; j < n; j++) {
if (board[i][j] != '.') {
if (row.count(board[i][j])) return false;
row.insert(board[i][j]);
}
}
}
for (int j = 0; j < n; j++) {
set<char> col;
for (int i = 0; i < n; i++) {
if (board[i][j] != '.') {
if (col.count(board[i][j])) return false;
col.insert(board[i][j]);
}
}
}
for (int boxRow = 0; boxRow < n; boxRow += 2) {
for (int boxCol = 0; boxCol < n; boxCol += 2) {
set<char> box;
for (int i = boxRow; i < boxRow + 2; i++) {
for (int j = boxCol; j < boxCol + 2; j++) {
if (board[i][j] != '.') {
if (box.count(board[i][j])) return false;
box.insert(board[i][j]);
}
}
}
}
}
return true;
}
int main() {
int n;
cin >> n;
vector<vector<char>> board(n, vector<char>(n));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> board[i][j];
}
}
if (isValidSudoku(board, n)) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
return 0;
}