s0036
This commit is contained in:
38
src/s0036_valid_sudoku.cpp
Normal file
38
src/s0036_valid_sudoku.cpp
Normal file
@@ -0,0 +1,38 @@
|
||||
#include "s0036_valid_sudoku.hpp"
|
||||
|
||||
bool Solution::isValidSudoku(vector<vector<char>>& board) {
|
||||
vector<unordered_map<char, int>> rows;
|
||||
vector<unordered_map<char, int>> columns;
|
||||
vector<unordered_map<char, int>> grids;
|
||||
// Initialize vectors
|
||||
for (int i{0}; i < 9; ++i) {
|
||||
rows.push_back(unordered_map<char, int>{});
|
||||
columns.push_back(unordered_map<char, int>{});
|
||||
grids.push_back(unordered_map<char, int>{});
|
||||
}
|
||||
for (int i{0}; i < 9; ++i) {
|
||||
for (int j{0}; j < 9; ++j) {
|
||||
if (board[i][j] == '.') {
|
||||
continue;
|
||||
}
|
||||
if (rows[j].count(board[i][j]) == 1) {
|
||||
return false;
|
||||
} else {
|
||||
rows[j][board[i][j]] = 1;
|
||||
}
|
||||
if (columns[i].count(board[i][j]) == 1) {
|
||||
return false;
|
||||
} else {
|
||||
columns[i][board[i][j]] = 1;
|
||||
}
|
||||
int gridIndex =
|
||||
static_cast<int>(floor(j / 3)) + 3 * static_cast<int>(floor(i / 3));
|
||||
if (grids[gridIndex].count(board[i][j]) == 1) {
|
||||
return false;
|
||||
} else {
|
||||
grids[gridIndex][board[i][j]] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
Reference in New Issue
Block a user