<p>Write a program to solve a Sudoku puzzle by filling the empty cells.</p>

<p>A sudoku solution must satisfy <strong>all of the following rules</strong>:</p>

<ol>
	<li>Each of the digits <code>1-9</code> must occur exactly once in each row.</li>
	<li>Each of the digits <code>1-9</code> must occur exactly once in each column.</li>
	<li>Each of the digits <code>1-9</code> must occur exactly once in each of the 9 <code>3x3</code> sub-boxes of the grid.</li>
</ol>

<p>The <code>&#39;.&#39;</code> character indicates empty cells.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/ff/Sudoku-by-L2G-20050714.svg/250px-Sudoku-by-L2G-20050714.svg.png" style="height:250px; width:250px" />
<pre>
<strong>Input:</strong> board = [[&quot;5&quot;,&quot;3&quot;,&quot;.&quot;,&quot;.&quot;,&quot;7&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;6&quot;,&quot;.&quot;,&quot;.&quot;,&quot;1&quot;,&quot;9&quot;,&quot;5&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;9&quot;,&quot;8&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;6&quot;,&quot;.&quot;],[&quot;8&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;6&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;3&quot;],[&quot;4&quot;,&quot;.&quot;,&quot;.&quot;,&quot;8&quot;,&quot;.&quot;,&quot;3&quot;,&quot;.&quot;,&quot;.&quot;,&quot;1&quot;],[&quot;7&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;2&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;6&quot;],[&quot;.&quot;,&quot;6&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;2&quot;,&quot;8&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;4&quot;,&quot;1&quot;,&quot;9&quot;,&quot;.&quot;,&quot;.&quot;,&quot;5&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;8&quot;,&quot;.&quot;,&quot;.&quot;,&quot;7&quot;,&quot;9&quot;]]
<strong>Output:</strong> [[&quot;5&quot;,&quot;3&quot;,&quot;4&quot;,&quot;6&quot;,&quot;7&quot;,&quot;8&quot;,&quot;9&quot;,&quot;1&quot;,&quot;2&quot;],[&quot;6&quot;,&quot;7&quot;,&quot;2&quot;,&quot;1&quot;,&quot;9&quot;,&quot;5&quot;,&quot;3&quot;,&quot;4&quot;,&quot;8&quot;],[&quot;1&quot;,&quot;9&quot;,&quot;8&quot;,&quot;3&quot;,&quot;4&quot;,&quot;2&quot;,&quot;5&quot;,&quot;6&quot;,&quot;7&quot;],[&quot;8&quot;,&quot;5&quot;,&quot;9&quot;,&quot;7&quot;,&quot;6&quot;,&quot;1&quot;,&quot;4&quot;,&quot;2&quot;,&quot;3&quot;],[&quot;4&quot;,&quot;2&quot;,&quot;6&quot;,&quot;8&quot;,&quot;5&quot;,&quot;3&quot;,&quot;7&quot;,&quot;9&quot;,&quot;1&quot;],[&quot;7&quot;,&quot;1&quot;,&quot;3&quot;,&quot;9&quot;,&quot;2&quot;,&quot;4&quot;,&quot;8&quot;,&quot;5&quot;,&quot;6&quot;],[&quot;9&quot;,&quot;6&quot;,&quot;1&quot;,&quot;5&quot;,&quot;3&quot;,&quot;7&quot;,&quot;2&quot;,&quot;8&quot;,&quot;4&quot;],[&quot;2&quot;,&quot;8&quot;,&quot;7&quot;,&quot;4&quot;,&quot;1&quot;,&quot;9&quot;,&quot;6&quot;,&quot;3&quot;,&quot;5&quot;],[&quot;3&quot;,&quot;4&quot;,&quot;5&quot;,&quot;2&quot;,&quot;8&quot;,&quot;6&quot;,&quot;1&quot;,&quot;7&quot;,&quot;9&quot;]]
<strong>Explanation:</strong>&nbsp;The input board is shown above and the only valid solution is shown below:

<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/3/31/Sudoku-by-L2G-20050714_solution.svg/250px-Sudoku-by-L2G-20050714_solution.svg.png" style="height:250px; width:250px" />
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>board.length == 9</code></li>
	<li><code>board[i].length == 9</code></li>
	<li><code>board[i][j]</code> is a digit or <code>&#39;.&#39;</code>.</li>
	<li>It is <strong>guaranteed</strong> that the input board has only one solution.</li>
</ul>

class Solution {
    static constexpr std::array<char, 10> kOptions = {'.', '1', '2', '3', '4', '5',
                                                     '6', '7', '8', '9'};

    bool ValidRow(const vector<vector<char>>& board, int row) {
        std::unordered_set<char> char_set;
        for (int col = 0; col < 9; col++) {
            if (board[row][col] == '.') {
                continue;
            }

            if (char_set.contains(board[row][col])) {
                return false;
            } else {
                char_set.insert(board[row][col]);
            }
        }

        return true;
    }

    bool ValidCol(const vector<vector<char>>& board, int col) {
        std::unordered_set<char> char_set;
        for (int row = 0; row < 9; row++) {
            if (board[row][col] == '.') {
                continue;
            }

            if (char_set.contains(board[row][col])) {
                return false;
            } else {
                char_set.insert(board[row][col]);
            }
        }

        return true;
    }

    bool ValidSquare(const vector<vector<char>>& board, int row, int col) {
        std::unordered_set<char> char_set;

        int start_row = 3 * (row / 3);
        int start_col = 3 * (col / 3);

        int end_row = start_row + 3;
        int end_col = start_col + 3;

        for (int cur_row = start_row; cur_row < end_row; cur_row++) {
            for (int cur_col = start_col; cur_col < end_col; cur_col++) {
                if (board[cur_row][cur_col] == '.') {
                    continue;
                }

                if (char_set.contains(board[cur_row][cur_col])) {
                    return false;
                } else {
                    char_set.insert(board[cur_row][cur_col]);
                }
            }
        }

        return true;
    }

    struct Node {
        int row;
        int col;
        char val;

        Node(int r, int c, char v) : row(r), col(c), val(v) {}
    };

public:
    void solveSudoku(vector<vector<char>>& board) {
        std::stack<Node> dfs_stack;

        std::vector<Node> empty_cells;

        for (int r = 0; r < 9; r++) {
            for (int c = 0; c < 9; c++) {
                if (board[r][c] != '.') {
                    continue;
                }

                empty_cells.push_back(Node(r, c, '.'));
            }
        }

        for (char c : kOptions) {
            Node n = empty_cells.front();
            n.val = c;
            dfs_stack.push(n);
        }

        while (!dfs_stack.empty()) {
            Node cur = dfs_stack.top();
            dfs_stack.pop();
            board[cur.row][cur.col] = cur.val;

            if (board[0][1] == '4') {
                int a = 0;
            }

            if (!ValidRow(board, cur.row) || !ValidCol(board, cur.col) ||
                !ValidSquare(board, cur.row, cur.col) || (cur.val == '.')) {
                continue;
            }

            bool has_unfilled = false;
            for (Node n : empty_cells) {
                if (board[n.row][n.col] != '.') {
                    continue;
                }

                has_unfilled = true;

                for (const char& c : kOptions) {
                    n.val = c;
                    dfs_stack.push(n);
                }

                break;
            }

            if (!has_unfilled) {
                return;
            }
        }
    }
};
189821