Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.03 KB | None | 0 0
  1. #include <iostream>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. typedef long long ll;
  5. typedef vector<int> veci;
  6. typedef vector<long long> vecll;
  7. typedef stack<int> stki;
  8. typedef stack<long long> stkll;
  9. typedef queue<int> qi;
  10. typedef queue<long long> qll;
  11. typedef deque<int> dqi;
  12. typedef deque<long long> dqll;
  13. typedef pair<int,int> pii;
  14. typedef pair<long long,long long> pll;
  15. typedef priority_queue<int> pqi;
  16. typedef priority_queue<long long> pqll;
  17. typedef string st;
  18.  
  19. bool isRight(int ar[9][9],int i,int j,int no){
  20.     int temp = i;
  21.     for (i = 0;i < 9;i++){
  22.         if(ar[i][j] == no){
  23.             return false;
  24.         }
  25.     }
  26.     i = temp;
  27.     temp = j;
  28.     for (j = 0;j < 9;j++){
  29.         if(ar[i][j] == no){
  30.             return false;
  31.         }
  32.     }
  33.     j = temp;
  34.     int temp1,temp2;
  35.     j = (j / 3)*3;
  36.     i = (i / 3)*3;
  37.     for (temp1 = 0;temp1<3;temp1++) {
  38.         for(temp2 = 0;temp2<3;temp2++) {
  39.             if (ar[i+temp1][j+temp2]==no){
  40.                 return false;
  41.             }
  42.         }
  43.     }
  44.     return true;
  45. }
  46.  
  47. bool solved = 0;
  48.  
  49. void solve(int ar[9][9],int i,int j){
  50. if(solved) {
  51.     return;
  52. }
  53. if(j==9){
  54.     i++;
  55.     if(i == 9){
  56.         solved = true;
  57.         return;
  58.     }
  59.     j = 0;
  60.     solve(ar,i,j);
  61. }
  62. if(ar[i][j]){
  63.     solve(ar,i,j+1);
  64. }
  65. else {
  66.     for(int k = 1;k<=9;k++){
  67.             if (isRight(ar,i,j,k)) {
  68.                 ar[i][j] = k;
  69.                 solve(ar,i,j+1);
  70.                 if(solved) {
  71.                     return;
  72.                 }
  73.                 ar[i][j] = 0;
  74.             }
  75.         }
  76.     }
  77. }
  78.  
  79. int main()
  80. {
  81.     string str;
  82.     int n;
  83.     cin>>n;
  84.     int ar[9][9];
  85.     while(n--){
  86.         for(int i = 0;i<9;i++){
  87.             cin>>str;
  88.             for(int j = 0;j<9;j++){
  89.                ar[i][j] = str[j] - '0';
  90.             }
  91.         }
  92.         solve(ar,0,0);
  93.         solved = 0;
  94.         for(int i = 0;i<9;i++){
  95.             for(int j = 0;j<9;j++){
  96.                 cout<<ar[i][j];
  97.             }
  98.             cout<<endl;
  99.         }
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement