#include <iostream>
#include <vector>
#include <unordered_set>
// Sudoku solution must satisfy all of the following rules:
// Each of the digits 1-9 must occur once in each row.
// Each of the digits 1-9 must occur once in each column.
// Each of the digits 1-9 must occur once in each 3x3 grid.
bool isValidSudoku3x3Grid(const std::vector<std::vector<int>>& grid) {
if (grid.size() != 3 || grid[0].size() != 3) return false; // Ensure it's a 3x3 grid
std::unordered_set<int> seen;
for (const auto& row : grid) {
for (int num : row) {
// seen.count(num) // not inserted yet = 0
if (num < 1 || num > 9 || seen.count(num)) {
return false; // Invalid if number is out of range or repeated
}
seen.insert(num);
}
}
return true; // Valid if all numbers 1-9 appear exactly once
}
int main() {
std::vector<std::vector<int>> grid = {
{5, 3, 4},
{6, 7, 2},
{1, 9, 8}
};
if (isValidSudoku3x3Grid(grid)) {
std::cout << "The grid is a valid Sudoku grid!" << std::endl;
} else {
std::cout << "The grid is NOT a valid Sudoku grid!" << std::endl;
}
}
/*
run:
The grid is a valid Sudoku grid!
*/