Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <math.h>
  4.  
  5. using namespace std;
  6. const int MAX_N = 10;
  7.  
  8. int size, idx;
  9. int grid[MAX_N][MAX_N];
  10. int id_area[MAX_N][MAX_N];
  11. bool area[MAX_N][10];
  12.  
  13. void get_areas_id(void)
  14. {
  15.     for (int i = 0; i < size; i += sqrt(size)) {
  16.         for (int j = 0; j < size; j += sqrt(size)) {
  17.             for (int k = i; k < i + sqrt(size); k++) {
  18.                 for (int l = j; l < j + sqrt(size); l++) {
  19.                     id_area[k][l] = idx;
  20.                 }
  21.             }
  22.             idx++;
  23.         }
  24.     }
  25.     // This is to quickly check if a number is already in an area
  26.     for (int i = 0; i < size; i++) {
  27.         for (int j = 0; j < size; j++) {
  28.             if (grid[i][j] != 0) {
  29.                 area[id_area[i][j]][grid[i][j]] = true;
  30.             }
  31.         }
  32.     }
  33.     return;
  34. }
  35.  
  36. bool can_place(int val, int x, int y)
  37. {
  38.     // Check current row
  39.     for (int i = 0; i < size; i++) {
  40.         if (grid[x][i] == val) {
  41.             return false;
  42.         }
  43.     }
  44.     // Check current column
  45.     for (int i = 0; i < size; i++) {
  46.         if (grid[i][y] == val) {
  47.             return false;
  48.         }
  49.     }
  50.     // Check current area
  51.     if (area[id_area[x][y]][val]) {
  52.         return false;
  53.     }
  54.     return true;
  55. }
  56.  
  57. bool empty_cell(int &x, int &y)
  58. {
  59.     for (x = 0; x < size; x++) {
  60.         for (y = 0; y < size; y++) {
  61.             if (grid[x][y] == 0) {
  62.                 return true;
  63.             }
  64.         }
  65.     }
  66.     return false;
  67. }
  68.  
  69. bool fill(void)
  70. {
  71.     int x, y;
  72.     if (!empty_cell(x, y)) { // Sudoku solved
  73.         return true;
  74.     }
  75.     for (int val = 1; val <= size; val++) {
  76.         if (can_place(val, x, y)) {
  77.             grid[x][y] = val;
  78.             area[id_area[x][y]][val] = true;
  79.  
  80.             if (fill()) {
  81.                 return true;
  82.             }
  83.  
  84.             // Reset
  85.             area[id_area[x][y]][val] = false;
  86.             grid[x][y] = 0;
  87.         }
  88.     }
  89.     return false;
  90. }
  91.  
  92. int main()
  93. {
  94.     ios::sync_with_stdio(false);
  95.     cin.tie(0); cout.tie(0);
  96.  
  97.     // Input
  98.     cin >> size;
  99.     for (int i = 0; i < size; i++) {
  100.         for (int j = 0; j < size; j++) {
  101.             cin >> grid[i][j];
  102.         }
  103.     }
  104.  
  105.     get_areas_id();
  106.     fill();
  107.  
  108.     for (int i = 0; i < size; i++) {
  109.         for (int j = 0; j < size; j++) {
  110.             cout << grid[i][j] << " ";
  111.         }
  112.         cout << endl;
  113.     }
  114.     return 0;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement