请教一道数独题本人最近做了一道数独题,算到只剩几个数字了,做了几天都没有结果,希望有高手可以继续帮我找出下一个数字,并请说明理由,我已经做到这样了,目前肯定是对的.320160548108405390

来源:学生作业帮助网 编辑:作业帮 时间:2024/05/01 07:45:08
请教一道数独题本人最近做了一道数独题,算到只剩几个数字了,做了几天都没有结果,希望有高手可以继续帮我找出下一个数字,并请说明理由,我已经做到这样了,目前肯定是对的.320160548108405390

请教一道数独题本人最近做了一道数独题,算到只剩几个数字了,做了几天都没有结果,希望有高手可以继续帮我找出下一个数字,并请说明理由,我已经做到这样了,目前肯定是对的.320160548108405390
请教一道数独题
本人最近做了一道数独题,算到只剩几个数字了,做了几天都没有结果,希望有高手可以继续帮我找出下一个数字,并请说明理由,我已经做到这样了,目前肯定是对的.
320160548
108405390
050308170
831506420
500040813
042813650
080931265
293654781
615080934
对不起,我不喜欢用假设法,我认为如果用“试”的方法来做,没什么意义,一个个试过去,当然到最后肯定有对的时候。

请教一道数独题本人最近做了一道数独题,算到只剩几个数字了,做了几天都没有结果,希望有高手可以继续帮我找出下一个数字,并请说明理由,我已经做到这样了,目前肯定是对的.320160548108405390
320 160 548
108 405 390
050 308 170
831 506 420
500 X4Y 813
042 813 650
080 931 265
293 654 781
615 X8X 934
为方便讲解看上图,标有X的三个空格处候选数是2,7,而标有Y的空格处候选数是2,7,9三个数,这是一个典型的矩阵删减,必须把Y处的2和7剔除,才能保证有唯一解(你可以这样去理解,如果选了2,那必然还可以选7,因为这两数是对称的,这样就会出现两个解,所以2,7肯定都不对,都需要剔除)所以Y处应该填入9,于是此题迎仞而解.这个题算是入门级,直观法之后稍有瓶颈,但难度不大,全题应该在十分钟内完成.

数独c语言算法
#include
#include
using namespace std;
int check_Num(int,int);
int check_Line(int ,int );
int check_Table(int ,int,int);
int check_Column(i...

全部展开

数独c语言算法
#include
#include
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<<"%%%%%%%%%%%%"<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< 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< cout< }
cout<<" "< }
}
// 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< 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 "< return 1;
}
}
// cout<<"no same "<< t<<" "< 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 "< return 1;
}}
}
//cout<<"9gongge,not found "< 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 "< return 1;
}
}
//cout<<"not found "< 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<<" "< }
for(int q=0;q<9;q++){
if(A[q][p]==t)
zero_number--;
}
}
// cout<<"after"<< t<<" "< 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< return 1;
}}
}
//cout<<"no found"< 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 "< 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 "< 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];
}
for(int m=0;m<9;m++){
if(A[p][m]==0&&counter==1)
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=sum-A[k][q];
}
for(int m=0;m<9;m++){
if(A[m][q]==0&&counter==1)
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++;}
}
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&&counter==1)
sum= sum-A[j][k];
}
}
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==1)
A[p][q]=sum;
}}
}
void check_only_two(int i,int j){
//cout< int x=i/3,y=j/3;
int bool_row=0,bool_col=0,bool_table=0;
int row_zero_num=0,col_zero_num=0,table_zero_num=0;
//check num of zero in col
for(int m=0;m<9;m++){
if(A[m][j]==0)
col_zero_num++;
}
//cout<<"number of zero in COL "< //check num of zero in row
for(int m=0;m<9;m++){
if(A[i][m]==0)
row_zero_num++;
}
//cout<<"number of zero in ROW "< //check num of zero of table
for(int p=3*x;p<3*x+3;p++){
for(int q=3*y;q<3+3*y;q++){
if(A[p][q]==0){
table_zero_num++;
//cout<<"number of zero in TABLE "< //cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"< }}
}
// cout< if(((0 //check if row has the same
for(int k=1;k<10;k++){
bool_row = check_Line(k,i);
//check if col has the same
bool_col = check_Column(k,j);
// check if table has the same
bool_table= checkSame_two_table(k,x,y);
if(bool_row==-1&&bool_col==-1&&bool_table==-1){
A[i][j]=k;
}
}
}
}
int checkSame_two_table(int t,int x,int y){
for(int c=3*x;c<3+3*x;c++){
for(int d=3*y;d<3+3*y;d++){
if(A[c][d]==t){
return 1;
}}}
return -1;
}

收起

3 2 a 1 6 b 5 4 8
1 c 8 4 d 5 3 9 e
f 5 g 3 h 8 1 7 i
8 3 1 5 j 6 4 2 k
5 L m n 4 o 8 1 3
p 4 2 8 1 3 6 5 q
r 8 s 9 3 1 2 6 5
2 9 3 6 5 4 7 8 1
6 1 5 t 8 u 9 3 4
...

全部展开

3 2 a 1 6 b 5 4 8
1 c 8 4 d 5 3 9 e
f 5 g 3 h 8 1 7 i
8 3 1 5 j 6 4 2 k
5 L m n 4 o 8 1 3
p 4 2 8 1 3 6 5 q
r 8 s 9 3 1 2 6 5
2 9 3 6 5 4 7 8 1
6 1 5 t 8 u 9 3 4
可知a为7或9,先设a为7,则b=9,推得j=9,q=9,则p=7,继之f=9,r=4,
s=7.但s=7与a=7矛盾。故a=9,b=7
即可依次推出t=7,u=2,o=9,n=2,j=7,k=9,q=7,p=9,c=7,L=6,h=9,d=2,
e=6,i=2,g=6,f=4,r=7,s=4,最后无疑m=7
故得到
3 2 9 1 6 7 5 4 8
1 7 8 4 2 5 3 9 6
4 5 6 3 9 8 1 7 2
8 3 1 5 7 6 4 2 9
5 6 7 2 4 9 8 1 3
9 4 2 8 1 3 6 5 7
7 8 4 9 3 1 2 6 5
2 9 3 6 5 4 7 8 1
6 1 5 7 8 2 9 3 4

收起

329167548
178425396
456398172
831576429
567249813
942813657
784931265
293654781
615782934
编程做的

329167548
178425396
456398172
567249813
942813657
784931265
293654781
615782934
我是用试误法做的~~