Advertisement
Guest User

sudoku.cpp

a guest
Jan 18th, 2018
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.13 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. using namespace std;
  5.  
  6. const int dim = 9;
  7.  
  8. bool cRiga (int schema[][dim], int riga, int n);
  9. bool cCol (int schema[][dim], int col, int n);
  10. bool cQuad (int schema[][dim], int x, int y, int n);
  11. void riempi(int schema[][dim]);
  12. void stampa(int schema[][dim]);
  13.  
  14. int main() {
  15.     srand(time(0));
  16.     int schema [dim][dim] = {{0}};
  17.     riempi(schema);
  18.    cout << endl;
  19.     stampa(schema);
  20.  
  21. }
  22.  
  23. bool cRiga (int schema[dim][dim], int riga, int n) {
  24.     for(int i = 0; i < dim; i++) {
  25.         if(schema[riga][i] == n) {
  26.             return false;
  27.         }
  28.     }
  29.     return true;
  30. }
  31.  
  32. bool cCol (int schema[dim][dim], int col, int n) {
  33.     for(int i = 0; i < dim; i++) {
  34.         if(schema[i][col] == n) {
  35.             return false;
  36.         }
  37.     }
  38.     return true;
  39. }
  40.  
  41. bool cQuad (int schema[][dim], int x, int y, int n) {
  42.    x = x - (x % 3);
  43.    y = y - (y % 3);
  44.     for (int i = x; i < x + 3; i++) {
  45.         for (int j = y; j < y + 3; j++) {
  46.             if(schema[i][j] == n) {
  47.                 return false;
  48.             }
  49.         }
  50.     }
  51.     return true;
  52. }
  53.  
  54. void riempi(int schema[dim][dim]) {
  55.     int n;
  56.     for(int i = 0; i < dim; i++) {
  57.         for(int j = 0; j < dim; j++) {
  58.          while (true) {
  59.             n = rand() % 9 + 1;
  60.  
  61.             // if (cRiga(schema, i, n)) { // controllo ISOLATO sulla riga: OK!
  62.             // if (cCol(schema, j, n)) { // controllo ISOLATO sulla colonna: OK!
  63.             // if (cQuad(schema, i, j, n)) { // controllo ISOLATO sul quadrato: OK!
  64.  
  65.             // va in loop con un range inferiore a 1-11
  66.             // if (cRiga(schema, i, n) && cCol(schema, j, n)) {
  67.  
  68.             // va in loop con un range inferiore a 1-15
  69.             if (cRiga(schema, i, n) && cCol(schema, j, n) && cQuad(schema, i, j, n)) {
  70.                     schema[i][j] = n;
  71.                cout << i << "," << j << endl;
  72.                break;
  73.             }
  74.             }
  75.  
  76.         }
  77.     }
  78. }
  79.  
  80. void stampa(int schema[][dim]) {
  81.     for (int i = 0; i < dim; i++) {
  82.         for (int j = 0; j < dim; j++) {
  83.             cout << schema[i][j] << " ";
  84.          if ((j+1) % 3 == 0 && j != 0) {
  85.             cout << " ";
  86.          }
  87.         }
  88.       if ((i+1) % 3 == 0 && i != 0 && i != dim-1) {
  89.          cout << endl;
  90.       }
  91.         cout << endl;
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement