#include <vector>
#include <iostream>
using namespace std;
class Solution {
private:
bool solve(vector<vector<char>> &board){
for(int i=0; i<9; i++){
for(int j=0; j<9; j++){
if(board[i][j] == '.'){
for(char ch='1'; ch<='9'; ch++){
if(isValid(board, i, j, ch)){
board[i][j] = ch;
if(solve(board))
return true;
else{
board[i][j] = '.';
}
}
}
return false;
}
}
}
return true;
}
bool isValid(vector<vector<char>>& board, int row, int col, char ch){
for(int i=0; i<9; i++){
if(board[row][i] == ch) return false;
if(board[i][col] == ch) return false;
if(board[3*(row/3) + i/3][3*(col/3) + i%3] == ch) return false;
}
return true;
}
public:
void solveSudoku(vector<vector<char>>& board) {
solve(board);
}
};
int main() {
Solution solution;
vector<vector<char>> board = {
{'5', '3', '.', '.', '7', '.', '.', '.', '.'},
{'6', '.', '.', '1', '9', '5', '.', '.', '.'},
{'.', '9', '8', '.', '.', '.', '.', '6', '.'},
{'8', '.', '.', '.', '6', '.', '.', '.', '3'},
{'4', '.', '.', '8', '.', '3', '.', '.', '1'},
{'7', '.', '.', '.', '2', '.', '.', '.', '6'},
{'.', '6', '.', '.', '.', '.', '2', '8', '.'},
{'.', '.', '.', '4', '1', '9', '.', '.', '5'},
{'.', '.', '.', '.', '8', '.', '.', '7', '9'}
};
solution.solveSudoku(board);
for (const auto& row : board) {
for (const auto& cell : row) {
cout << cell << " ";
}
cout << endl;
}
return 0;
}
I2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwpjbGFzcyBTb2x1dGlvbiB7CnByaXZhdGU6CiAgICBib29sIHNvbHZlKHZlY3Rvcjx2ZWN0b3I8Y2hhcj4+ICZib2FyZCl7ICAgCiAgICAgICAgZm9yKGludCBpPTA7IGk8OTsgaSsrKXsKICAgICAgICAgICAgZm9yKGludCBqPTA7IGo8OTsgaisrKXsKICAgICAgICAgICAgICAgIGlmKGJvYXJkW2ldW2pdID09ICcuJyl7CiAgICAgICAgICAgICAgICAgICAgZm9yKGNoYXIgY2g9JzEnOyBjaDw9JzknOyBjaCsrKXsKICAgICAgICAgICAgICAgICAgICAgICAgaWYoaXNWYWxpZChib2FyZCwgaSwgaiwgY2gpKXsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGJvYXJkW2ldW2pdID0gY2g7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZihzb2x2ZShib2FyZCkpCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcmV0dXJuIHRydWU7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBlbHNlewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGJvYXJkW2ldW2pdID0gJy4nOyAgIAogICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIHJldHVybiBmYWxzZTsKICAgICAgICAgICAgICAgIH0KCiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgcmV0dXJuIHRydWU7CiAgICB9CgogICAgYm9vbCBpc1ZhbGlkKHZlY3Rvcjx2ZWN0b3I8Y2hhcj4+JiBib2FyZCwgaW50IHJvdywgaW50IGNvbCwgY2hhciBjaCl7CiAgICAgICAgZm9yKGludCBpPTA7IGk8OTsgaSsrKXsKICAgICAgICAgICAgaWYoYm9hcmRbcm93XVtpXSA9PSBjaCkgcmV0dXJuIGZhbHNlOwogICAgICAgICAgICBpZihib2FyZFtpXVtjb2xdID09IGNoKSByZXR1cm4gZmFsc2U7CiAgICAgICAgICAgIGlmKGJvYXJkWzMqKHJvdy8zKSArIGkvM11bMyooY29sLzMpICsgaSUzXSA9PSBjaCkgcmV0dXJuIGZhbHNlOwogICAgICAgIH0KCiAgICAgICAgcmV0dXJuIHRydWU7CiAgICB9CgpwdWJsaWM6CiAgICB2b2lkIHNvbHZlU3Vkb2t1KHZlY3Rvcjx2ZWN0b3I8Y2hhcj4+JiBib2FyZCkgewogICAgICAgIHNvbHZlKGJvYXJkKTsKICAgIH0KfTsKCmludCBtYWluKCkgewogICAgU29sdXRpb24gc29sdXRpb247CiAgICB2ZWN0b3I8dmVjdG9yPGNoYXI+PiBib2FyZCA9IHsKICAgICAgICB7JzUnLCAnMycsICcuJywgJy4nLCAnNycsICcuJywgJy4nLCAnLicsICcuJ30sCiAgICAgICAgeyc2JywgJy4nLCAnLicsICcxJywgJzknLCAnNScsICcuJywgJy4nLCAnLid9LAogICAgICAgIHsnLicsICc5JywgJzgnLCAnLicsICcuJywgJy4nLCAnLicsICc2JywgJy4nfSwKICAgICAgICB7JzgnLCAnLicsICcuJywgJy4nLCAnNicsICcuJywgJy4nLCAnLicsICczJ30sCiAgICAgICAgeyc0JywgJy4nLCAnLicsICc4JywgJy4nLCAnMycsICcuJywgJy4nLCAnMSd9LAogICAgICAgIHsnNycsICcuJywgJy4nLCAnLicsICcyJywgJy4nLCAnLicsICcuJywgJzYnfSwKICAgICAgICB7Jy4nLCAnNicsICcuJywgJy4nLCAnLicsICcuJywgJzInLCAnOCcsICcuJ30sCiAgICAgICAgeycuJywgJy4nLCAnLicsICc0JywgJzEnLCAnOScsICcuJywgJy4nLCAnNSd9LAogICAgICAgIHsnLicsICcuJywgJy4nLCAnLicsICc4JywgJy4nLCAnLicsICc3JywgJzknfQogICAgfTsKICAgIHNvbHV0aW9uLnNvbHZlU3Vkb2t1KGJvYXJkKTsKICAgIGZvciAoY29uc3QgYXV0byYgcm93IDogYm9hcmQpIHsKICAgICAgICBmb3IgKGNvbnN0IGF1dG8mIGNlbGwgOiByb3cpIHsKICAgICAgICAgICAgY291dCA8PCBjZWxsIDw8ICIgIjsKICAgICAgICB9CiAgICAgICAgY291dCA8PCBlbmRsOwogICAgfQogICAgCiAgICByZXR1cm4gMDsKfQoK