<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 {
public:


    bool solve(vector<vector<char>>& board,vector<vector<unordered_set<int>>> &subset,vector<unordered_set<int>> &row,vector<unordered_set<int>> &col,int r, int n){

        for(int j=0;j<n;++j){
            if(board[r][j]=='.'){
                for(int i=1;i<=n;++i){
                if(r==3 && j==7 && board[3][0]=='8' && board[3][1]=='5' && board[3][2]=='9' && board[3][3]=='7' && board[3][4]=='6' && board[3][5]=='1' && board[3][6]=='4' && board[3][7]=='.' && board[3][8]=='3'){
                    for(int k=0;k<n;++k){
                        cout<<board[0][k];
                    }
                    for(int k=0;k<n;++k){
                        cout<<board[1][k];
                    }
                    for(int k=0;k<n;++k){
                        cout<<board[2][k];
                    }
                    cout<<" ";
                    for(auto &ele:row[r]){
                        cout<<ele;
                    }
            cout<<endl<<endl;
        }
                    if(row[r].find(i)==row[r].end() && subset[r/3][j/3].find(i)==subset[r/3][j/3].end() && col[j].find(i)==col[j].end()){
        //             if(r==3 && board[2][0]=='1' && board[2][1]=='9' && board[2][2]=='8' && board[2][3]=='3' && board[2][4]=='4' && board[2][5]=='2' && board[2][6]=='5' && board[2][7]=='6' && board[2][8]=='7'){
        //     for(int i=0;i<n;++i){
        //         cout<<board[r][i]<<" ";
        //     }
        //     cout<<endl;
        // }
                        row[r].insert(i);
                        col[j].insert(i);
                        subset[r/3][j/3].insert(i);
                        board[r][j]='0'+i;
                        bool solved = false;
                        if(row[r].size()==n){
                            if(r+1 == n){
                                return true;
                            }

                            solved = solve(board,subset,row,col,r+1,n);
                            // if(r==2 && j+1==n && board[2][0]=='1' && board[2][1]=='9' && board[2][2]=='8' && board[2][3]=='3' && board[2][4]=='4' && board[2][5]=='2' && board[2][6]=='5' && board[2][7]=='6' && board[2][8]=='7' ){

                            //     for(int k=0;k<n;++k){
                            //         cout<<board[1][k];
                            //     }
                            //     cout<<endl;
                            // }


                        }else{
                            solved = solve(board,subset,row,col,r,n);
                        }
                        if(solved){
                            return solved;
                        }

                        row[r].erase(i);
                        col[j].erase(i);
                        subset[r/3][j/3].erase(i);
                        board[r][j]='.';
                    }
                }
                return false;
            }
        }

        return false;
    }

    void solveSudoku(vector<vector<char>>& board) {
        int n=board.size();
        vector<unordered_set<int>> row(n),col(n);
        vector<vector<unordered_set<int>>> subset(n/3,vector<unordered_set<int>>(n/3));
        unordered_set<int> temp;
        for(int i=0;i<n;++i){
            for(int j=0;j<n;++j){
             if(board[i][j]!='.'){
                subset[i/3][j/3].insert(board[i][j]-'0');
                row[i].insert(board[i][j]-'0');
                col[j].insert(board[i][j]-'0');
             }   
            }
        }

        solve(board,subset,row,col,0,n);
        return;

    }
};
21315