fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. class Solution {
  7. public:
  8. vector<vector<int>> MeraSudoku;
  9. bool rowUsed[9][10]{};
  10. bool colUsed[9][10]{};
  11. bool boxUsed[9][10]{};
  12.  
  13. int getBoxIndex(int row, int col) {
  14. return (row / 3) * 3 + (col / 3);
  15. }
  16.  
  17. bool solve() {
  18. for (int i = 0; i < 9; ++i) {
  19. for (int j = 0; j < 9; ++j) {
  20. if (MeraSudoku[i][j] == 0) {
  21. for (int num = 1; num <= 9; ++num) {
  22. int boxIdx = getBoxIndex(i, j);
  23. if (!rowUsed[i][num] && !colUsed[j][num] && !boxUsed[boxIdx][num]) {
  24. MeraSudoku[i][j] = num;
  25. rowUsed[i][num] = colUsed[j][num] = boxUsed[boxIdx][num] = true;
  26.  
  27. if (solve()) return true;
  28.  
  29. MeraSudoku[i][j] = 0;
  30. rowUsed[i][num] = colUsed[j][num] = boxUsed[boxIdx][num] = false;
  31. }
  32. }
  33. return false; // dacă niciun număr nu merge
  34. }
  35. }
  36. }
  37. return true; // complet rezolvat
  38. }
  39.  
  40. void solveSudoku(vector<vector<char>>& board) {
  41. MeraSudoku.resize(9, vector<int>(9, 0));
  42. for (int i = 0; i < 9; ++i) {
  43. for (int j = 0; j < 9; ++j) {
  44. if (board[i][j] != '.') {
  45. int num = board[i][j] - '0';
  46. MeraSudoku[i][j] = num;
  47. int boxIdx = getBoxIndex(i, j);
  48. rowUsed[i][num] = true;
  49. colUsed[j][num] = true;
  50. boxUsed[boxIdx][num] = true;
  51. }
  52. }
  53. }
  54.  
  55. solve();
  56.  
  57. for (int i = 0; i < 9; ++i)
  58. for (int j = 0; j < 9; ++j)
  59. board[i][j] = MeraSudoku[i][j] + '0';
  60. }
  61. };
  62.  
  63. // Funcție de afișare
  64. void printBoard(const vector<vector<char>>& board) {
  65. for (int i = 0; i < 9; ++i) {
  66. for (int j = 0; j < 9; ++j)
  67. cout << board[i][j] << ' ';
  68. cout << '\n';
  69. }
  70. }
  71.  
  72. int main() {
  73. vector<vector<char>> board = {
  74. {'5','3','.','.','7','.','.','.','.'},
  75. {'6','.','.','1','9','5','.','.','.'},
  76. {'.','9','8','.','.','.','.','6','.'},
  77. {'8','.','.','.','6','.','.','.','3'},
  78. {'4','.','.','8','.','3','.','.','1'},
  79. {'7','.','.','.','2','.','.','.','6'},
  80. {'.','6','.','.','.','.','2','8','.'},
  81. {'.','.','.','4','1','9','.','.','5'},
  82. {'.','.','.','.','8','.','.','7','9'}
  83. };
  84.  
  85. cout << "Initial Board:\n";
  86. printBoard(board);
  87.  
  88. Solution solver;
  89. solver.solveSudoku(board);
  90.  
  91. cout << "\nSolved Board:\n";
  92. printBoard(board);
  93.  
  94. return 0;
  95. }
  96.  
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
Initial 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 

Solved Board:
5 3 4 6 7 8 9 1 2 
6 7 2 1 9 5 3 4 8 
1 9 8 3 4 2 5 6 7 
8 5 9 7 6 1 4 2 3 
4 2 6 8 5 3 7 9 1 
7 1 3 9 2 4 8 5 6 
9 6 1 5 3 7 2 8 4 
2 8 7 4 1 9 6 3 5 
3 4 5 2 8 6 1 7 9