Advertisement
What_Ever

Untitled

Feb 3rd, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.23 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <cmath>
  5. #include <string>
  6. #include <stdio.h>
  7.  
  8. using namespace std;
  9. int N;
  10. vector< vector<int> > mat(9, vector<int>(9,0));
  11. vector<vector<bool> > col(9,vector<bool>(9,false)),row(9,vector<bool>(9,false));
  12. vector< vector< vector<bool> > > sq(3,vector<vector<bool> >(3,vector<bool>(3,false)));
  13. vector< pair<int,int> > emp;
  14.  
  15. void printMat()
  16. {
  17.     for(int counter = 0; counter < 9; counter++)
  18.     {
  19.         for(int counter2 = 0; counter2 < 9; counter2++)
  20.         {
  21.             cout << mat[counter][counter2] << " ";
  22.         }
  23.         cout << endl;
  24.     }
  25. }
  26. void solve(int n)
  27. {
  28.     if(n == N)
  29.     {
  30.         printMat();
  31.         return;
  32.     }
  33.     pair<int,int> p = emp[n];
  34.     for(int counter = 0; counter < 9; counter++)
  35.     {
  36.         if(!row[p.first][counter] && !col[p.second][counter] && !sq[p.first/3][p.second/3][counter])
  37.         {
  38.             row[p.first][counter] = true;
  39.             col[p.second][counter] = true;
  40.             sq[p.first/3][p.second/3][counter] = true;
  41.             mat[p.first][p.second] = counter+1;
  42.             solve(n+1);
  43.             mat[p.first][p.second] = 0;
  44.             row[p.first][counter] = false;
  45.             col[p.second][counter] = false;
  46.             sq[p.first/3][p.second/3][counter] = false;
  47.         }
  48.     }
  49. }
  50. int main()
  51. {
  52.     pair<int,int> p;
  53.     for(int counter = 0; counter < 9; counter++)
  54.     {
  55.         for(int counter2 = 0; counter2 < 9; counter2++)
  56.         {
  57.             row[counter][counter2] = false;
  58.             col[counter][counter2] = false;
  59.         }
  60.     }
  61.     for(int counter = 0; counter < 9; counter++)
  62.     {
  63.         for(int counter2 = 0; counter2 < 9; counter2++)
  64.         {
  65.             cin >> mat[counter][counter2];
  66.             if( mat[counter][counter2] == 0)
  67.             {
  68.                 p.first = counter;
  69.                 p.second = counter2;
  70.                 emp.push_back(p);
  71.             }else
  72.             {
  73.                 row[counter][mat[counter][counter2]-1] = true;
  74.                 col[counter2][mat[counter][counter2]-1] = true;
  75.                 sq[counter/3][counter2/3][mat[counter][counter2]-1] = true;
  76.             }
  77.         }
  78.     }
  79.     N = emp.size();
  80.     solve(0);
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement