正文

数独程序代码2006-05-08 09:12:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/yexin218/13695.html

分享到:

在苦苦熬了两天两夜之后,我的程序终于有点成形了!虽然还不能解决所有的数独问题(像有只靠‘三三定为 法,唯一法都还无法解决的)。我的程序好长,一点也不简短!但是也是我一个字敲出来的!现在贴出来与大 家分享!希望有人能够帮我解决我还没有解决的问题或者提一个建议!        你的鼓励就是前进的动力!#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_line(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); int A[9][9] =    {  {1,2,0,0,0,3,4,0,5},    {3,0,6,0,5,0,0,8,0},    {0,4,0,2,0,0,0,0,7},    {0,0,7,0,1,0,0,0,8},    {0,6,0,5,0,2,0,3,0},    {5,0,0,0,8,0,9,0,0},    {7,0,0,0,0,1,0,9,0},    {0,8,0,0,4,0,1,0,3},    {9,0,1,7,0,0,0,5,4}   };    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<10;w++){cout<<"%%%%%%%%%%%%"<<endl;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_line(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];           }       }        }        }    }   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);   }}}   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;  }} return 0;} //check firse three table ,the same number is >2int 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; } int check_column_table(int t,int m,int n){   int col= n/3;   for(int p=3*m;p<3*m+3;p++){   for(int q= 3*col;q<3*col+3;q++){          if(A[p][q]==t){           // cout<<t<<"  "<<p<<"  "<<q<<endl;            return 1;         }}   }         //cout<<"no found"<<t<<"  "<<col<<endl;   return -1; }int check_column_line(int t,int l){      for(int p=0;p<9;p++){       if(A[p][l]== t){        // cout<< t<<"   "<<p<<"  "<< l<<endl;         return 1;      }    }return -1;} int check_column_num_zero(int r,int l){    int num_col_zero =0;    for(int m = 3*r;m<3*r+3;m++){       if(A[m][l]==0){          num_col_zero++;       }    }    //cout<<"number_zero   "<<num_col_zero<<endl;    return num_col_zero;}int check_column_samenum(int t,int r){    int c = r/3;    int col_same_num=0;   for(int i =0;i<9;i++){      for(int j=3*c;j<3+3*c;j++){         if(A[i][j]==t){            col_same_num++;         }}}      //cout<<"same num   "<<t<<"  "<<col_same_num<<endl;         return col_same_num;} int check_which_table_line(int t,int r,int c){   int counter_zero =0;   for(int p = 3*c;p<3+3*c;p++){      if(A[r][p]==0){         counter_zero++;        for(int q=0;q<9;q++){          if(A[q][p]==t)          counter_zero--;       }}}       return counter_zero;       }int check_which_table_column(int t,int r,int c){   int counter_zero=0;    for(int p =3*r;p<3+3*r;p++){       if(A[p][c]==0){          counter_zero++;          for(int q=0;q<9;q++){             if(A[p][q]==t)             counter_zero--;          }}}          return counter_zero;}void check_only_line(int p){       int sum=45;       int counter=0;   for(int q=0;q<9;q++){        if(A[p][q]==0){           counter++;        }}        for(int k=0;k<9;k++){        if(counter==1&&A[p][k]!=0)         sum= sum-A[p][k];        }        if(counter==1){        for(int m=0;m<9;m++){          if(A[p][m]==0)          A[p][m]= sum;       }}       } void check_only_col(int q){   int sum=45;int counter=0;   for(int p=0;p<9;p++){      if(A[p][q]==0){         counter++;      }}      for(int k=0;k<9;k++){         if(counter==1&&A[k][q]!=0)            sum-=A[k][q];      }      if(counter==1){         for(int m=0;m<9;m++){            if(A[m][q]==0)               A[m][q]=sum;            }}      }void check_nine_table(int m,int n){   int sum=45;int counter=0;   for(int p=3*m;p<3*m+3;p++){      for(int q=3*n;q<3*n+3;q++){         if(A[p][q]==0)         counter++;}   }      if(counter==1){         for(int j=3*m;j<3*m+3;j++){      for(int k=3*n;k<3*n+3;k++){         if(A[j][k]!=0)          sum-=A[j][k];      }         }}         if(counter==1){      for(int p=3*m;p<3*m+3;p++){      for(int q=3*n;q<3*n+3;q++){         if(A[p][q]==0)         A[p][q]=sum;      }}         }}这个程序的运行结果是:1 2 9 8 7 3 4 6 53 7 6 1 5 4 2 8 98 4 5 2 6 9 3 1 72 9 7 3 1 6 5 4 84 6 8 5 9 2 7 3 15 1 3 4 8 7 9 2 67 5 4 6 3 1 8 9 26 8 2 9 4 5 1 7 39 3 1 7 2 8 6 5 4 可惜的是像这种得数独:{3,0,0,0,0,5,1,0,9},{0,0,0,0,1,0,6,7,0},{1,0,6,4,0,0,0,0,0},{5,2,1,0,0,8,0,0,7},{8,4,7,5,9,1,3,2,0},{6,0,0,7,0,0,8,5,1},{0,0,0,0,0,6,2,1,0},{0,1,8,0,5,0,0,0,0},{2,6,0,1,0,0,0,0,8}这种跑出来的结果是:3 0 0 0 0 5 1 0 90 0 0 0 1 0 6 7 01 0 6 4 0 0 0 0 05 2 1 0 0 8 0 0 78 4 7 5 9 1 3 2 66 0 0 7 0 0 8 5 10 0 0 0 0 6 2 1 00 1 8 0 5 0 0 6 02 6 0 1 0 0 0 0 8好像什么也不做!这让我好郁闷·但我知道问题出在哪里,只是这个我很在考虑中!两天熬下来,真的让我疲 惫!欢迎大家提个意见如果碰到每一行或者列支剩下两个空没有填出来,有什么好的方法?QQ:331387429

阅读(6078) | 评论(4)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

loading...
您需要登录后才能评论,请 登录 或者 注册