Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.92 KB | None | 0 0
  1. void wpisywanieSudokuDoRozwiazania(){
  2.     int a;
  3. for (int j=0;j<21;j++){
  4.     for (int i=0;i<9;i++){
  5.         cin >> a;
  6.         sudoku[j][i]=a;
  7.     }
  8. }
  9. }
  10.  
  11. bool solve(int x, int y);
  12.  
  13. int sudoku[9][9] = { /* tu zadane sudoku... */ };
  14. int curr[9][9];
  15. bool can_insert(int x, int y, int value) {
  16.     for(int i = 0; i < 9; i++) {
  17.         if (value == curr[x][i] || value == curr[i][y] ||
  18.             value == curr[x/3*3+i%3][y/3*3+i/3]) return false;
  19.     } return true;
  20. }
  21.  
  22. bool next(int x, int y) {
  23.     if (x == 8 && y == 8) return true;
  24.     else if (x == 8) return solve(0, y + 1);
  25.     else return solve(x + 1, y);
  26. }
  27.  
  28. bool solve(int x, int y) {
  29.     if (sudoku[x][y] == 0) {
  30.         for(int i = 1; i <= 9; i++) {
  31.             if (can_insert(x, y, i)) {
  32.                 curr[x][y] = i;
  33.                 if (next(x, y)) return true;
  34.             }
  35.         } curr[x][y] = 0; return false;
  36.     } return next(x, y);
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement