// 演算法與 C 版相同：一次掃描，對每個數字檢查其列/行/小方塊是否重複。
// Same algorithm as the C version: one pass, check row/col/box duplication per digit.
// 這裡用 std::array 包成固定大小的二維表，型別安全且不需手動管理記憶體。
// Here we use std::array as fixed-size 2D tables — type-safe and no manual memory management.

#include <vector>
#include <array>
#include <string>
using namespace std;

class Solution {
public:
    bool isValidSudoku(vector<vector<char>>& board) {
        // std::array<bool,10> 是固定長度 10 的布林陣列；外層 array<...,9> 給每列/行/方塊各一份。
        // std::array<bool,10> is a fixed length-10 bool array; the outer array<...,9> gives one per row/col/box.
        // 大括號 {} 會把所有元素初始化成 false（值初始化），不必再手動清零。
        // The braces {} value-initialize every element to false, so no manual clearing is needed.
        array<array<bool, 10>, 9> rows{};
        array<array<bool, 10>, 9> cols{};
        array<array<bool, 10>, 9> boxes{};

        for (int i = 0; i < 9; ++i) {
            for (int j = 0; j < 9; ++j) {
                char c = board[i][j];        // 這一格的字元 / this cell's char
                if (c == '.') continue;      // 空格跳過 / skip empty cells

                int d = c - '0';             // 字元轉數字 1..9 / char to int 1..9
                int b = (i / 3) * 3 + (j / 3); // 小方塊編號 0..8 / box id 0..8（整數除法 / integer division）

                // 任一張表已標記 → 出現重複 → 棋盤無效。
                // Any table already marked → duplicate → invalid board.
                if (rows[i][d] || cols[j][d] || boxes[b][d]) {
                    return false;
                }

                // 標記三張表，記錄「此數字已在這列/行/方塊出現」。
                // Mark all three tables: this digit now seen in this row/col/box.
                rows[i][d] = cols[j][d] = boxes[b][d] = true;
            }
        }

        return true; // 沒有任何衝突 / no conflicts found
    }
};
