|
#include <iostream> #include <iomanip> using namespace std; int check_Num(int,int); int check_Line(int ,int ); int check_Table(int ,int,int); int check_Column(int ,int ); int check_EachLine_Num(int,int,int); int check_column_table(int ,int ,int );
int check_column_num_zero(int ,int ); int check_column_samenum(int ,int ); int check_which_table_line(int ,int ,int ); int check_which_table_column(int ,int ,int ); void check_only_line(int p); void check_only_col(int q); void check_nine_table(int m,int n); void check_only_two(int i,int j); int checkSame_two_table(int t,int x,int y); int A[9][9] = { {1,0,0,8,3,0,0,0,2}, {5,7,0,0,0,1,0,0,0}, {0,0,0,5,0,9,0,6,4}, {7,0,4,0,0,8,5,9,0}, {0,0,3,0,1,0,4,0,0}, {0,5,1,4,0,0,3,0,6}, {3,6,0,7,0,4,0,0,0}, {0,0,0,6,0,0,0,7,9}, {8,0,0,0,5,2,0,0,3} };
int row,column, rowc,columnc; int c[3]={0}; int d[3]={0}; //int g[3]={0};
int NO=0; int e[3]={0}; int f[3] ={0}; int h[3]={0};
int main() { for(int w=0;w<3;w++){ cout<<"%%%%%%%%%%%%"<<endl; for(int v=0;v<3;v++){ for(int i=0;i<9;i++){ for(int j=0;j<9;j++){ int num = check_Num(A[i][j],i);
if(A[i][j]!=0&&num==2){
// if(num==2){
for(int k =0;k<3;k++){ c[k] = check_Line(A[i][j],3*(i/3)+k); d[k] = check_Table(A[i][j],i,k); }
for(int k=0;k<3;k++){ //cout<<c[k]<<" "<<d[k]<<endl;
if(c[k]<0) row = 3*(i/3)+k; if(d[k]<0) column = k; }
for(int b = 3*column;b<3*column+3;b++){ int bool_value = check_Column(A[i][j],b); int zero_num= check_which_table_line(A[i][j],row, column); if(A[row][b]==0&&bool_value<0&&zero_num==1){ A[row][b] = A[i][j]; } } }
} }
for(int i=0;i<9;i++){ for(int j=0;j<9;j++){ int num_same= check_column_samenum(A[i][j],j);
if(A[i][j]!=0&&num_same==2){
for(int k = 0;k<3;k++){ e[k] = check_column_table(A[i][j],k,j); f[k]= check_Column(A[i][j],(3*(j/3))+k); }
for(int k=0;k<3;k++){ if(e[k]<0) rowc =k; if(f[k]<0) columnc=3*(j/3)+k; } for(int h= 3*rowc;h<3+3*rowc;h++){ int bool_value = check_Line(A[i][j],h); int zero_col_num = check_which_table_column(A[i][j],rowc,columnc);
if(A[h][columnc]==0&&bool_value<0&&zero_col_num==1){ A[h][columnc]=A[i][j]; }
} } } }
} // the method of only one
for(int t=0;t<3;t++){ for(int w=0;w<9;w++){ check_only_line(w);
} for(int w=0;w<9;w++){ check_only_col(w); } for(int s=0;s<3;s++){ for(int d=0;d<3;d++){ check_nine_table(s,d); } } } //the methond of only two
for(int t=0;t<3;t++){ for(int i=0;i<9;i++){ for(int j=0;j<9;j++){ if(A[i][j]==0){ check_only_two(i,j); } } } }
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){ NO++; if(NO%9==1) cout<<endl; cout<<setw(2)<<A[i][j]<<" ";
} cout<<" "<<endl; } }
// do judgement to check wehcher the array is right
return 0; }
//check firse three table ,the same number is >2
int check_Num(int t,int x){ int counter=0; int r = x/3; for(int m=3*r;m<3*r+3;m++){ for(int n=0;n<9;n++){ if(A[m][n]==t){ counter++; } } } //cout<<t<<" "<<counter<<" "<<r<<endl;
return counter; } //check whick line deesn't have corruent number
int check_Line(int t,int m){ //if(t==0)
//return 0;
for(int n =0;n<9;n++){ if(A[m][n]==t){ //cout<<"found "<<t<<" "<<endl;
return 1; } }
// cout<<"no same "<< t<<" "<<endl;
return -1;
} //check which table have the corruent number
int check_Table(int t,int m,int n){ int r = m/3; for(int p = 3*r;p<3*r+3;p++){ for(int q =3*n;q<3+3*n;q++){ if(A[p][q]==t){ // cout<<"9gongge "<<t<<" "<<p<<" "<<q<<endl;
return 1; }} } //cout<<"9gongge,not found "<<t<<" "<< r<<endl;
return -1; } //check the corruent col have same number
int check_Column(int t,int m){ for(int i =0;i<9;i++){ if(A[i][m]== t){ //cout<<"found column "<<t<<" "<<m<<" "<<i<<endl;
return 1; } } //cout<<"not found "<<t<<" "<<m<<endl;
return -1; } int check_EachLine_Num(int t,int r,int c){ int zero_number=0; for(int p=3*c;p<3*c+3;p++){ if(A[r][p]==0){ zero_number++; //cout<< "before "<< t<<" "<<zero_number<<endl;
} for(int q=0;q<9;q++){ if(A[q][p]==t) zero_number--; } } // cout<<"after"<< t<<" "<<zero_number<<endl;
return zero_number;
}
|