Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- typedef vector<int> veci;
- typedef vector<long long> vecll;
- typedef stack<int> stki;
- typedef stack<long long> stkll;
- typedef queue<int> qi;
- typedef queue<long long> qll;
- typedef deque<int> dqi;
- typedef deque<long long> dqll;
- typedef pair<int,int> pii;
- typedef pair<long long,long long> pll;
- typedef priority_queue<int> pqi;
- typedef priority_queue<long long> pqll;
- typedef string st;
- bool isRight(int ar[9][9],int i,int j,int no){
- int temp = i;
- for (i = 0;i < 9;i++){
- if(ar[i][j] == no){
- return false;
- }
- }
- i = temp;
- temp = j;
- for (j = 0;j < 9;j++){
- if(ar[i][j] == no){
- return false;
- }
- }
- j = temp;
- int temp1,temp2;
- j = (j / 3)*3;
- i = (i / 3)*3;
- for (temp1 = 0;temp1<3;temp1++) {
- for(temp2 = 0;temp2<3;temp2++) {
- if (ar[i+temp1][j+temp2]==no){
- return false;
- }
- }
- }
- return true;
- }
- bool solved = 0;
- void solve(int ar[9][9],int i,int j){
- if(solved) {
- return;
- }
- if(j==9){
- i++;
- if(i == 9){
- solved = true;
- return;
- }
- j = 0;
- solve(ar,i,j);
- }
- if(ar[i][j]){
- solve(ar,i,j+1);
- }
- else {
- for(int k = 1;k<=9;k++){
- if (isRight(ar,i,j,k)) {
- ar[i][j] = k;
- solve(ar,i,j+1);
- if(solved) {
- return;
- }
- ar[i][j] = 0;
- }
- }
- }
- }
- int main()
- {
- string str;
- int n;
- cin>>n;
- int ar[9][9];
- while(n--){
- for(int i = 0;i<9;i++){
- cin>>str;
- for(int j = 0;j<9;j++){
- ar[i][j] = str[j] - '0';
- }
- }
- solve(ar,0,0);
- solved = 0;
- for(int i = 0;i<9;i++){
- for(int j = 0;j<9;j++){
- cout<<ar[i][j];
- }
- cout<<endl;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement