编程之美-构造数独

(1)在9×9的数组中心随机生成一个3×3的中心数组。

(2)已3×3中心数组为对称进行循环置换。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>


int main()
{
    int matrix[3][3];
    int i;
    int j;
    int shudu[9][9]={0};

    for (i=0; i<3; ++i)
    {
        for (j=0; j<3; ++j)
        {
            matrix[i][j]=i*3+j+1;
        }
    }

    /*随机初始化矩阵*/
    srand(time(0)); 
    for (i=0; i<9; i++)
    {
        int temp=rand()%9;
        j=matrix[i/3][i%3];
        matrix[i/3][i%3]=matrix[temp/3][temp%3];
        matrix[temp/3][temp%3]=j;        
    }

    for (i=0; i<3; ++i)
    {
        for (j=0; j<3; ++j)
        {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }

    /* 利用置换的方法组合数独 */

    for (i=0; i<3; ++i)
    {
        for (j=0; j<3; ++j)
        {
            shudu[i+3][j+3]=matrix[i][j];
            if (0==i)
            {
                shudu[i+5][j+6]=shudu[i+3][j+3];
                shudu[i+4][j]=shudu[i+3][j+3];
            }
            else if (1==i)
            {
                shudu[i+2][j+6]=shudu[i+3][j+3];
                shudu[i+4][j]=shudu[i+3][j+3];
            }
            else
            {
                shudu[i+2][j+6]=shudu[i+3][j+3];
                shudu[i+1][j]=shudu[i+3][j+3];
            }

        }
    }

    
    for (j=0; j<3; ++j)
    {
        for (i=0; i<3; ++i)
        {
            if (0==j)
            {
                shudu[i][j+4]=shudu[i+3][j+3];
                shudu[i+6][j+5]=shudu[i+3][j+3];
            }
            else if (1==j)
            {
                shudu[i][j+4]=shudu[i+3][j+3];
                shudu[i+6][j+2]=shudu[i+3][j+3];
            }
            else
            {
                shudu[i][j+1]=shudu[i+3][j+3];
                shudu[i+6][j+2]=shudu[i+3][j+3];
            }
            
        }
    }


    for (j=0; j<3; ++j)
    {
        for (i=0; i<3; ++i)
        {
            if (0==j)
            {
                shudu[i][j+1]=shudu[i+3][j];
                shudu[i+6][j+2]=shudu[i+3][j];
            }
            else if (1==j)
            {
                shudu[i][j+1]=shudu[i+3][j];
                shudu[i+6][j-1]=shudu[i+3][j];
            }
            else
            {
                shudu[i][j-2]=shudu[i+3][j];
                shudu[i+6][j-1]=shudu[i+3][j];
            }
            
        }
    }

    
    for (j=0; j<3; ++j)
    {
        for (i=0; i<3; ++i)
        {
            if (0==j)
            {
                shudu[i][j+7]=shudu[i+3][j+6];
                shudu[i+6][j+8]=shudu[i+3][j+6];
            }
            else if (1==j)
            {
                shudu[i][j+7]=shudu[i+3][j+6];
                shudu[i+6][j+5]=shudu[i+3][j+6];
            }
            else
            {
                shudu[i][j+4]=shudu[i+3][j+6];
                shudu[i+6][j+5]=shudu[i+3][j+6];
            }
            
        }
    }

    
    /*  输出数独数组  */
    printf("输出数独数组:\n");
    for (i=0; i<9; ++i)
    {
        for (j=0; j<9; ++j)
        {
            printf("%d ", shudu[i][j]);
        }
        printf("\n");
    }

}

 

posted @ 2012-05-09 10:41  徐露  阅读(1099)  评论(0编辑  收藏  举报