数独(sudoku)实现

发在校内上了,想想还是贴到这来更适合些:

 

看《编程之美》,没什么事,自己写了个数独程序

回溯非递归,贴上来吧

怕是过些时候就没了。

不知道从linux下贴过来有没有贴错...

 

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <assert.h>
#include <string.h>
                                                                                                                 
//generate a maxtrix whose volumn is (m*m) * (m*m)
int ** generate_sudoku( int m )
{
    if( m < 1 )
        printf("m must be greater than zero!/n");
                                                                                                                 
    int n = m * m;
    int * num_used_in_cur_row = new int [n];
    int ** num_used_in_cur_unit_matrix = new int* [m];
    int * candidate_num_row_idx_array = new int [n];
    int ** sudoku_matrix = new int* [n];
                                                                                                                 
    int i, j;
    for( i = 0; i < m; i++ )
    {
        num_used_in_cur_unit_matrix[i] = new int [n];
        memset( num_used_in_cur_unit_matrix[i], 0, ( sizeof( int) * n) );
    }
                                                                                                                 
    //initial the sudoku_matrix
    //fill each column with num 1-n randmonly
    //the conflict in each row and unit matrix, if any, will be resolved later
    for( i = 0; i < n; i++ )
    {
        sudoku_matrix[i] = new int [n];
        memset( sudoku_matrix[i], 0, ( sizeof( int) * n) );
    }
    srand( time( NULL ) );
    int row, column;
    for( column = 0; column < n; column++ )

    {
        int num = 1;
        row = 0;
        for( num; num <= n; num++ )
        {
           row = rand() % n;
           //use open-address way to find an available position for current num
           while( sudoku_matrix[row][column] != 0 )
                row = (row + 1) % n;
           sudoku_matrix[row][column] = num;
        }
    }
                                                                                                                 
    printf(" The initial sudoku-matrix is:/n" );
    for( i = 0; i < n; i++ )
    {
        for( j = 0; j < n; j++ )
            printf( "%d/t", sudoku_matrix[i][j] );
        printf("/n");
    }
                                                                                                                 
                                                                                                                 
    //deal with the conflict in each row and unit matrix now
    //using backtracing algorithm
    //candidate_num_row_idx_array keeps the info for each column while trace back
    //the complexity should be O( n*n*n ) but I didn't prove it accuratlly
    int cur_unit_matrix_row = 0;
    for( row = 0; row < n; row++ )
    {
        column = 0;
        //initial job for resolve this row
        memset( num_used_in_cur_row, 0, ( sizeof( int ) * n ) );                                                 
        if( row == cur_unit_matrix_row + m )
        {
            for( i = 0; i < m; i++ )
                memset( num_used_in_cur_unit_matrix[ i ], 0, ( sizeof( int ) * n ) );
            cur_unit_matrix_row = row;
        }
        for( i = 0; i < n; i++ )
            candidate_num_row_idx_array[ i ] = row;
                                                                                                                 
        //resolve conflict now
        while( column < n )
        {
            int unit_matrix_idx = column / m;
            int candidate_row = candidate_num_row_idx_array[ column ];
                                                                                                                 
            //find a right num for this position from this column
            while( candidate_row < n && /
                   ( num_used_in_cur_row[ sudoku_matrix[candidate_row][column] - 1 ] != 0 || /
                     num_used_in_cur_unit_matrix[unit_matrix_idx][ sudoku_matrix[candidate_row][column] - 1 ] != 0)
                 )
            {
                candidate_row++;
            }
                                                                                                                 
            // we need to trace back and try another way if current situation is locked
            if( candidate_row >= n )
            {
                //trace back current column's status
                candidate_num_row_idx_array[ column ] = row;

                                                                                                                 
                //deal with the former column
                column--;
                assert( column >= 0 );
                unit_matrix_idx = column / m;
                //mark back the candidate num unused
                num_used_in_cur_row[ sudoku_matrix[candidate_num_row_idx_array[column]][column] - 1 ] = 0;
                num_used_in_cur_unit_matrix[unit_matrix_idx][ sudoku_matrix[ candidate_num_row_idx_array[column] ][column] - 1 ] = 0;
                //re-search from the next element
                candidate_num_row_idx_array[ column ]++;
                continue;
            }
                                                                                                                 
            candidate_num_row_idx_array[ column ] = candidate_row;//record the current candidate row index
            //mark this num used
            num_used_in_cur_row[ sudoku_matrix[candidate_row][column] - 1] = 1;
            num_used_in_cur_unit_matrix[unit_matrix_idx][ sudoku_matrix[candidate_row][column] - 1 ] = 1;
                                                                                                                 
            column++;
        }
        //Only exchange element here for efficiency
        for( column = 0; column < n; column++ )
        {
            if( row != candidate_num_row_idx_array[ column ] )
            {
                int temp = sudoku_matrix[row][column];
                sudoku_matrix[row][column] = sudoku_matrix[ candidate_num_row_idx_array[column] ][column];
                sudoku_matrix[ candidate_num_row_idx_array[column] ][column] = temp;
            }
//            printf( "%d/t", sudoku_matrix[row][column] );
        }
//        printf( "/n" );
    }
                                                                                                                 
    delete [] num_used_in_cur_row;
    for( i = 0; i < m; i++ )
        delete [] num_used_in_cur_unit_matrix[i];
    delete [] num_used_in_cur_unit_matrix;
    delete [] candidate_num_row_idx_array;
                                                                                                                 
    return sudoku_matrix;
}
                                                                                                                 
int main()
{
    int m, n;
    printf("Pls input the unit matrix dimension of sudoku you want: ");
    scanf("%d", &m);
    n = m * m;
    int ** sudoku_matrix = generate_sudoku( m );
                                                                                                                 
    printf(" The sudoku-matrix is:/n" );
    int i, j;
    for( i = 0; i < n; i++ )
    {
        for( j = 0; j < n; j++ )
            printf( "%d/t", sudoku_matrix[i][j] );
                                                                                                                 
        printf("/n");
    }
                                                                                                                 
    for( i = 0; i < n; i++ )
        delete [] sudoku_matrix[i];
    delete [] sudoku_matrix;
    return 0;
}

运行结果:

Pls input the unit matrix dimension of sudoku you want: 3
 The initial sudoku-matrix is:
8       1       1       9       3       5       5       3       6
3       7       5       5       5       6       8       2       1
9       8       2       4       4       2       1       6       4
1       4       3       6       6       7       2       1       5
5       9       8       7       7       9       7       8       7
6       2       6       3       8       4       9       9       8
4       6       4       8       1       1       6       4       9
2       3       7       1       9       8       4       7       3
7       5       9       2       2       3       3       5       2
 The sudoku-matrix is:
8       1       5       9       3       6       2       4       7
3       7       2       5       4       1       8       6       9
9       4       6       7       8       2       1       3       5
1       8       3       6       7       9       5       2       4
5       9       4       3       2       8       7       1       6
6       2       7       4       1       5       9       8       3
4       6       8       1       5       7       3       9       2
2       5       9       8       6       3       4       7       1
7       3       1       2       9       4       6       5       8

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一、数独说明:数独由九行,九列组成,又分为顺序排列的九宫。每行、每列及每宫都包含九个格,九个格中填放1到9的不重复的数字。 二、自动计算原理(三步法): 1、基础法:找出空格中唯一可填的数字。方法是,先假设某空格中可填入九个数字,然后去掉所在行、所在列、所在宫中的已有数字。余下如果是唯一一个数字,那么这个数字就是结果 2、找唯一法:例如果某行、某列或某宫中只剩一个空格,那么九个数字中缺少的那个就是结果。 3、求唯余法:对于存在多个可能值的空格,循环取其中一个作为假设值,然后反复利用方法1和方法2去测试,如果出错冲突或导致别的空格无值可填时,说明假设的值是错误的。并对别剩余未找到唯一值的空格进行同样操作,直至找到可行的一个方案。 三、自动出题,是自动求解的反向过程,先给出答案,再组合题目: 1、数独难易程度跟数独已知的数字个数有一定关系,但不是必然关系。可分为四级,根据网友“数独难度分级”的文章https://wenku.baidu.com/view/af550ed51a37f111f1855ba0.html,结果是分布在0到1之间的一系列值,值越少越容易。 2、出题时,先利用随机数往81个空格中填入不冲突的值。方法是,因为对角线的三宫中的数字互不干扰,用随机数填充这三宫,然后根据数独规则要求随机填入另外六宫。 3、这是最终结果,然后根据难易要求,随机将结果中的一定数量(可以用随机数)的方格清空。数独题已经形成。再根据网友提供的级别计算公式,计算形成的数独题的难易程度是否符合要求。(此时的数独答案不是唯一的) 4、难易程度具体计算公式是:两个空格所有可能值如果有依赖关系值为1,没依赖关系值为0。如此汇总所有空格之间的关系值为A,再除以空格个数B的18倍。即A/(18*B)。0—0.25为0级,0.25—0.5为1级,0.5—0.75为2级,0.75—1为3组。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值