标题:C语言解数独代码求教
只看楼主
不想被发现
Rank: 2
等 级:论坛游民
帖 子:7
专家分:17
注 册:2017-2-7
结帖率:100%
 问题点数:0 回复次数:0 
C语言解数独代码求教
#include"stdio.h"
#include"conio.h"

static     int n[2][81];
int   i=0;
int number( int i);                  
int jiancha();
void main()
{  
      
    FILE *fp;
    int  a, b, x, e=0;
      int  i1=0, j1=0;

    //printf("请新建文件\"du.txt\",并在其中输入已知元素!");
    //printf("\n输入格式(第一行第一个元素为例):\"11x\"(无\"\"号,x代表已知元素值)!\n");                     
    printf("请完成下列数度,0代表空格:");

    if((fp=fopen("du.txt","r"))==NULL)
    {
        printf("can not open this file,press any key to exit...");
        printf("\n文件du.txt不存在!");
        getch();
        exit(0);
    }
    for(;!feof(fp);)  //从du.txt中读取数字
    {
        fscanf(fp,"%1d%1d%1d",&a,&b,&x);
        n[0][(a-1)*9+b-1] = x;
        n[1][(a-1)*9+b-1] = 1;
        //printf("%3d",x);                              
    }
    fclose(fp);      //关闭txt文件

   
    printf("\n");
    for( i=0 ; i<81 ; ++i )    //打印txt中的数,形成题目
    {
        printf("%3d",n[0][i]);
        if( i%9 == 8 )
            printf("\n");
    }
    printf("\n请按任意键显示正确答案!");
    getch();
    printf("\n");        
    if(jiancha() == 0)         /*  判断所给题目同行、同列或小九宫中是否存在数字重复 */
    {
        printf("输入有误!\n请检查!");
        getch();
        exit(0);
    }                          

    for( i=0 ; -1<i && i<81 ; ++i)  /*  求符合条件的最小数字 */
    {
        if( n[1][i]    == 1 )
            continue;
        x = number(i);
        if( x != 0)
            continue;           
        for( ;  ; --i )
        {
            if( n[0][i] == 9 && n[1][i] == 0 )
                n[0][i] = 0;
            if( n[0][i] == 0 || n[1][i] == 1 )
                continue;
            break;
        }
        --i;
        if(i <=-2 )
        {
            printf("\n无解!!\n\n请按键退出!!");
            getch();
        }

    }

    printf("\n");
    for( i=0 ; i<81 ; i++)      //输出正确答案
    {
        printf("%3d",n[0][i]);
        if( i%9 == 8 )
            printf("\n");
    }
    getch();
   
}

int number( int i)                /*  求符合条件的最小数字 */
{
    int x = 1, a, b, c, d;
    a = i/9;
    b =    i%9;
    for(++n[0][i];    n[0][i]<=9;  n[0][i]++)
    {
        x=1;
        for( d = 0 ; d<9 ; d++ )
        {   
            if(n[0][i-b+d] == 0 || d == b )
                continue;
            if(n[0][i-b+d] == n[0][i])
            {
                x =    0;
                break;
            }                       
        }
        if( x == 0 )
            continue;               
        for( c = 0 ; c<9; ++c )
        {
            if(n[0][9*c+b] == 0 || a == c )
                continue;
            if(n[0][9*c+b] == n[0][i])
            {
                x =    0;
                break;
            }
        }
        if( x == 0 )
            continue;
        for( c=a/3*3 ; c<a/3*3+3 ; c++)
        {
            for( d=b/3*3; d<b/3*3+3 ; d++ )
            {
                if(n[0][9*c+d] == 0 || a == c && b == d )
                    continue;
                if( n[0][9*c+d] == n[0][i] )
                {
                    x=0;
                    break;
                }
            }                        
        if(x==0)
            break;
        }               
        if(x==1)
            break;
    }
    if(x==0)        
        (n[0][i]=0);
    return n[0][i];

}

        
int jiancha()            /*  判断所给题目同行、同列或小九宫中是否存在数字重复 */     
{
    int x = 1, a, b, c, d;
    int i=0;

   
    for( ; i<81; ++i )
    {        
        a = i/9;
        b =    i%9;
        if( n[1][i] == 0    )
            continue;
        for( d = 0 ; d<9 ; d++ )
        {   
            if(n[1][i-b+d] == 0 || d == b )
                continue;
            if(n[0][i-b+d] == n[0][i])
            {
                x =    0;     
                printf(" %d%d&%d%d ",a+1,b+1,a+1,(i-b+d)%9+1);
                break;
            }                       
        }
        if( x == 0 )
            break;               
        for( c = 0 ; c<9; ++c )
        {
            if(n[1][9*c+b] == 0 || a == c )
                continue;
            if(n[0][9*c+b] == n[0][i])
            {
                x =    0;        
                printf(" %d%d&%d%d ",a+1,b+1,(9*c+b)/9+1,b+1);
                break;
            }
        }
        if( x == 0 )
            break;
        for( c=a/3*3 ; c<a/3*3+3 ; c++)
        {
            for( d=b/3*3; d<b/3*3+3 ; d++ )
            {
                if(n[1][9*c+d] == 0 || a == c && b == d )
                    continue;
                if( n[0][9*c+d] == n[0][i] )
                {
                    x=0;            
                    printf(" %d%d&%d%d ",a+1,b+1,c+1,d+1);
                    break;
                }
            }                        
        if(x==0)
            break;
        }
        if(x==0)
            break;
    }
    return x;
}


这个代码是用来解数独的,有几个地方不大明白:
for(;!feof(fp);)  //从du.txt中读取数字
    {
        fscanf(fp,"%1d%1d%1d",&a,&b,&x);
        n[0][(a-1)*9+b-1] = x;
        n[1][(a-1)*9+b-1] = 1;
        //printf("%3d",x);                        
并且求教这个代码是如何计算出各个空格处数字的
搜索更多相关主题的帖子: include number C语言 新建 元素 
2017-02-21 22:17



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-474418-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.209215 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved