数独高难度解题技巧难度系数10的有什么啊?

题目:1 0 0 4 0 0 8 0 0
0 4 0 0 3 0 0 0 9
0 0 9 0 0 6 0 5 0
0 5 0 3 0 0 0 0 0
0 0 0 0 0 1 6 0 0
0 0 0 0 7 0 0 0 2
0 0 4 0 1 0 9 0 0
7 0 0 8 0 0 0 0 4
0 2 0 0 0 4 0 8 0唯一答案:
1
6
5
4
9
7
8
2
3
2
4
7
5
3
8
1
6
9
8
3
9
1
2
6
4
5
7
6
5
1
3
4
2
7
9
8
3
7
2
9
8
1
6
4
5
4
9
8
6
7
5
3
1
2
5
8
4
2
1
3
9
7
6
7
1
6
8
5
9
2
3
4
9
2
3
7
6
4
5
8
1代码:// 作者:Hello
// 链接:https://www.zhihu.com/question/477125068/answer/2045517457
// 来源:知乎
// 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
#include<stdio.h>
#include<stdlib.h>
int cc=0;
int sort(int x[][9],int x1[][9]);
void prt(int x[][9]);
int main()
{
int x[9][9],i,j,ans[9][9]={0},flag;
FILE *fp;
if((fp=fopen("data.txt","r"))==NULL){
printf("Can't open file!\n");
exit(1);
}
for(i=0;i<9;i++){
for(j=0;j<9;j++){
fscanf(fp,"%d",&x[i][j]);
}
}
fclose(fp);
flag=sort(x,ans);
if(flag==0){
printf("total=%d\n",cc);
}
return 0;
}
void prt(int x[][9])
{
int i,j;
for(i=0;i<9;i++){
for(j=0;j<9;j++){
printf("%3d",x[i][j]);
}
printf("\n");
}
}
int sort(int x[][9],int x1[][9])
{
int source[9][9][9],i,j,k,i1,j1,k1,cnt[9][9],a,count=0,count1=0,temp[9][9];
for(i=0;i<9;i++){
for(j=0;j<9;j++){
cnt[i][j]=9;
temp[i][j]=x[i][j];
for(k=0;k<9;k++){
source[i][j][k]=k+1;
}
}
}//初始化
for(i=0;i<9;i++){
for(j=0;j<9;j++){
if(a=x[i][j]){
count++;
for(k=0;k<9;k++){
if(k!=a-1) source[i][j][k]=0;
}
cnt[i][j]=1;//把确定的位置的其他可能性去除。
for(i1=0;i1<9;i1++){
if(i1!=i){
if(source[i1][j][a-1]!=0){
source[i1][j][a-1]=0;
cnt[i1][j]-=1;
}
}
}//把同一列其他重复的可能性去除。
for(j1=0;j1<9;j1++){
if(j1!=j){
if(source[i][j1][a-1]!=0){
source[i][j1][a-1]=0;
cnt[i][j1]-=1;
}
}
}//把同一行其他重复的可能性去除。
for(i1=i/3*3;i1<=i/3*3+2;i1++){
for(j1=j/3*3;j1<=j/3*3+2;j1++){
if(i1!=i&&j1!=j){
if(source[i1][j1][a-1]!=0){
source[i1][j1][a-1]=0;
cnt[i1][j1]-=1;
}
}
}
}//把同一方格其他重复的可能性去除。
}
}
}
for(i=0;i<9;i++){
for(j=0;j<9;j++){
if(cnt[i][j]==0){
return 0;
}//有可能出错的情况
if(cnt[i][j]==1){//若有确定的格子
if(temp[i][j]==0){
for(k=0;k<9;k++){
if(source[i][j][k]!=0){
temp[i][j]=k+1;
break;
}
}
}
count1++;//代表现在确定的格子
}
}
}
if(count1==81){
for(i=0;i<9;i++){
for(j=0;j<9;j++){
x1[i][j]=temp[i][j];
}
}
return 1;
}
else if(count1>count){
if(sort(temp,x1))
return 1;
return 0;
}
else{
int min=9,imark=0,jmark=0;
for(i=0;i<9;i++){
for(j=0;j<9;j++){
if(cnt[i][j]<min&&cnt[i][j]!=1){
imark=i;jmark=j;min=cnt[i][j];
}
}
}//找出可能性最少的格子并一一试验。
for(k=0;k<9;k++){
if(source[imark][jmark][k]!=0){
temp[imark][jmark]=k+1;
if(sort(temp,x1)){
prt(x1);
printf("\n");
cc++;
}
}
}
return 0;
}
}

我要回帖

更多关于 数独高难度解题技巧 的文章

 

随机推荐