Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <vector>
- #include <cmath>
- #include <string>
- #include <stdio.h>
- using namespace std;
- int N;
- vector< vector<int> > mat(9, vector<int>(9,0));
- vector<vector<bool> > col(9,vector<bool>(9,false)),row(9,vector<bool>(9,false));
- vector< vector< vector<bool> > > sq(3,vector<vector<bool> >(3,vector<bool>(3,false)));
- vector< pair<int,int> > emp;
- void printMat()
- {
- for(int counter = 0; counter < 9; counter++)
- {
- for(int counter2 = 0; counter2 < 9; counter2++)
- {
- cout << mat[counter][counter2] << " ";
- }
- cout << endl;
- }
- }
- void solve(int n)
- {
- if(n == N)
- {
- printMat();
- return;
- }
- pair<int,int> p = emp[n];
- for(int counter = 0; counter < 9; counter++)
- {
- if(!row[p.first][counter] && !col[p.second][counter] && !sq[p.first/3][p.second/3][counter])
- {
- row[p.first][counter] = true;
- col[p.second][counter] = true;
- sq[p.first/3][p.second/3][counter] = true;
- mat[p.first][p.second] = counter+1;
- solve(n+1);
- mat[p.first][p.second] = 0;
- row[p.first][counter] = false;
- col[p.second][counter] = false;
- sq[p.first/3][p.second/3][counter] = false;
- }
- }
- }
- int main()
- {
- pair<int,int> p;
- for(int counter = 0; counter < 9; counter++)
- {
- for(int counter2 = 0; counter2 < 9; counter2++)
- {
- row[counter][counter2] = false;
- col[counter][counter2] = false;
- }
- }
- for(int counter = 0; counter < 9; counter++)
- {
- for(int counter2 = 0; counter2 < 9; counter2++)
- {
- cin >> mat[counter][counter2];
- if( mat[counter][counter2] == 0)
- {
- p.first = counter;
- p.second = counter2;
- emp.push_back(p);
- }else
- {
- row[counter][mat[counter][counter2]-1] = true;
- col[counter2][mat[counter][counter2]-1] = true;
- sq[counter/3][counter2/3][mat[counter][counter2]-1] = true;
- }
- }
- }
- N = emp.size();
- solve(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement