数独暴力遍历代码

简介: 还是递归大法好。 #include #include #include #define CELL_DEEP 3#define MATRIX_DEEP 9#define MATRIX_NUM (MATRIX_DEEP*MATRIX_DEEP)#de...

还是递归大法好。

 

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

#define CELL_DEEP        3
#define MATRIX_DEEP      9
#define MATRIX_NUM      (MATRIX_DEEP*MATRIX_DEEP)
#define FULL_BITMAP     0x1FF


#define TEST_BITMAP(bitmap, bit)       (bitmap) & (1 << (bit))

int GET_BIT_NUM(int bitmap, int *last_bit)
{
    int bit_num = 0;
    int i;
    
    for(i = 0; i < MATRIX_DEEP; i++) 
    {
        if(TEST_BITMAP(bitmap, i)) 
        {
            if(NULL != last_bit) *last_bit = i;
            bit_num++;
        }
    }
    
    return bit_num;
}

int BITMAP_TO_STR(int bitmap, char *buffer)
{
    int i, j;
    
    if(NULL == buffer) return -1;

    for(i = 0, j = 0; i < MATRIX_DEEP; i++) {
        if(TEST_BITMAP(bitmap, i)) {
            buffer[j++] = '1' + i;
        }
    }
    buffer[j] = 0;

    return 0;
}

#define CLEAR_BITMAP(node, bit)   (node.bitmap) &= ~(1 << (bit)) //0-8
        
#define CHECK_BITMAP(node) \
    do { \
        int last_bit; \
        if(GET_BIT_NUM(node.bitmap, &last_bit) == 1) \
        node.value = last_bit + 1; \
    }while(0)

int my_init_data1[] = 
{
    0, 6, 0, 0, 0, 3, 0, 7, 0,
    0, 0, 0, 0, 1, 5, 0, 0, 3,
    0, 7, 9, 0, 2, 0, 0, 5, 0,
    0, 0, 3, 8, 0, 2, 0, 0, 7,
    4, 0, 8, 0, 3, 0, 5, 0, 2,
    6, 0, 0, 5, 0, 1, 9, 0, 0,
    0, 5, 0, 0, 6, 0, 7, 8, 0,
    7, 0, 0, 1, 5, 0, 0, 0, 0,
    0, 3, 0, 2, 0, 0, 0, 4, 0
};

int my_init_data2[] = 
{
    0, 7, 0, 0, 5, 0, 0, 6, 0,
    4, 0, 0, 9, 0, 3, 0, 0, 1,
    0, 0, 8, 0, 0, 0, 3, 0, 0,
    0, 5, 0, 0, 0, 0, 0, 4, 0,
    1, 0, 0, 0, 0, 0, 0, 0, 9,
    0, 2, 0, 0, 0, 0, 0, 1, 0,
    0, 0, 4, 0, 0, 0, 7, 0, 0,
    9, 0, 0, 1, 0, 7, 0, 0, 6,
    0, 8, 0, 0, 3, 0, 0, 5, 0
};

int my_init_data3[] = 
{
    0, 0, 0, 0, 0, 0, 0, 2, 0,
    0, 3, 0, 0, 0, 9, 0, 0, 6,
    0, 0, 1, 0, 4, 7, 0, 0, 0,
    0, 0, 0, 1, 0, 0, 4, 7, 0,
    0, 0, 5, 0, 0, 0, 3, 0, 0,
    0, 2, 7, 0, 0, 8, 0, 0, 0,
    0, 0, 0, 5, 3, 0, 8, 0, 0,
    8, 0, 0, 2, 0, 0, 0, 6, 0,
    0, 1, 0, 0, 0, 0, 0, 0, 0,
};

int my_init_data[] = 
{
    0, 0, 0, 0, 0, 0, 2, 5, 0,
    0, 0, 0, 0, 8, 9, 0, 0, 7,
    0, 0, 0, 2, 0, 0, 0, 0, 8,
    0, 0, 4, 0, 6, 0, 0, 1, 0,
    0, 6, 0, 9, 0, 4, 0, 3, 0,
    0, 7, 0, 0, 2, 0, 5, 0, 0,
    2, 0, 0, 0, 0, 6, 0, 0, 0,
    3, 0, 0, 5, 1, 0, 0, 0, 0,
    0, 8, 9, 0, 0, 0, 0, 0, 0,
};


typedef struct tagNODE_INFO_ST
{
    int value; // 0 means not sure
    int bitmap;
}NODE_INFO_ST;

NODE_INFO_ST my_node_db[MATRIX_NUM];

#if 1 // common
int get_init_data()
{
    int  i;
    char ch;
    
    printf("\n input initial data(y/n): ");
    scanf("%c", &ch);
    if ( ch != 'y' ) return 0;

INPUT_REP:  
    for(i = 0; i < MATRIX_DEEP; i++) {
        printf("line %d: ", i + 1);
        scanf("%d %d %d %d %d %d %d %d %d", 
            &my_init_data[i*MATRIX_DEEP], &my_init_data[i*MATRIX_DEEP+1], &my_init_data[i*MATRIX_DEEP+2],
            &my_init_data[i*MATRIX_DEEP+3], &my_init_data[i*MATRIX_DEEP+4], &my_init_data[i*MATRIX_DEEP+5],
            &my_init_data[i*MATRIX_DEEP+6], &my_init_data[i*MATRIX_DEEP+7], &my_init_data[i*MATRIX_DEEP+8]);
    }
    
    for(i = 0; i < MATRIX_NUM; i++) {
        if(my_init_data[i] < 0 || my_init_data[i] > MATRIX_DEEP) {
            printf("illegal data of %d, try again\n", i);
            goto INPUT_REP;
        }
    }
    
    return 0;
}

int init_node_db(NODE_INFO_ST *node_db, int *init_data)
{
    int i;
    
    assert(node_db != NULL);
    assert(init_data != NULL);
    
    for(i=0; i < MATRIX_NUM; i++) {
        if (init_data[i] > MATRIX_DEEP || init_data[i] < 0) return -2;
        
        if(init_data[i] > 0) {
            node_db[i].value = init_data[i];
            node_db[i].bitmap = 1 << (init_data[i] - 1);
        } else {
            node_db[i].value = 0;
            node_db[i].bitmap = FULL_BITMAP;
        }
    }
    
    return 0;
}

int show_init_data(int *init_data)
{
    int i;
    
    assert(init_data != NULL);
    
    printf("init data: \n");
    for(i = 0; i < MATRIX_NUM; i++) {
        printf("%d ", init_data[i]);
        if((i+1)%MATRIX_DEEP == 0)printf("\n");
    }
    return 0;    
}

int get_all_checked_num(NODE_INFO_ST *node_db)
{
    int checked_num = 0;
    int i;
    
    assert(node_db != NULL);
    
    for(i = 0; i < MATRIX_NUM; i++) {
        if (node_db[i].value > 0) {
            checked_num++;
        }        
    }
    
    return checked_num;    
}

int get_all_bit_num(NODE_INFO_ST *node_db)
{
    int bit_num = 0;
    int i;
    
    assert(node_db != NULL);
    
    for(i = 0; i < MATRIX_NUM; i++) {
        bit_num += GET_BIT_NUM(node_db[i].bitmap, NULL);
    }
    
    return bit_num;    
}

int show_sub_node_db(int line, NODE_INFO_ST *node_db)
{
    int i;
    char buffer[16];
    
    assert(node_db != NULL);
    
    printf("sub node db: ", line);
    for(i = 0; i < MATRIX_DEEP; i++) {
        if (node_db[i].value > 0) {
            printf("<==%d==> ", node_db[i].value);
       } else {
            BITMAP_TO_STR(node_db[i].bitmap, buffer);
            printf("%7s ", buffer);
        }
    }
    printf("\n");
    
    return 0;
}


int show_node_db(int line, NODE_INFO_ST *node_db)
{
    int i;
    char buffer[16];
    
    assert(node_db != NULL);
    
    printf("node db: line(%d) checked(%d)\n", line, get_all_checked_num(node_db));
    for(i = 0; i < MATRIX_NUM; i++) {
        if (node_db[i].value > 0) {
            printf("<==%d==> ", node_db[i].value);
       } else {
            BITMAP_TO_STR(node_db[i].bitmap, buffer);
            printf("%7s ", buffer);
        }
        if((i+1)%MATRIX_DEEP == 0)printf("\n");
    }
    
    return 0;
}


int load_sub_node_db(int mode, NODE_INFO_ST *node_db, int i, NODE_INFO_ST* sub_list)
{
    int j, k, row;
    int cell_x, cell_y;
    
    assert(node_db != NULL);
    assert(sub_list != NULL);

    if(mode == 1) // row(i)
    {
        for (j = i*MATRIX_DEEP, k = 0; j < i*MATRIX_DEEP + MATRIX_DEEP; j++) {
            memcpy(&sub_list[k++], &node_db[j], sizeof(NODE_INFO_ST));
        }    
    }
    else if (mode == 2) // col(i)
    {
        for (j = i, k = 0; j < MATRIX_NUM; j+= MATRIX_DEEP) {
            memcpy(&sub_list[k++], &node_db[j], sizeof(NODE_INFO_ST));
        }
    }
    else if (mode == 3) // cell(i)
    {
        cell_x = (i/CELL_DEEP)*CELL_DEEP;
        cell_y = (i%CELL_DEEP)*CELL_DEEP;
        for(row = 0, k = 0; row < CELL_DEEP; row++) {
                for (j = (cell_x + row)*MATRIX_DEEP + cell_y; j < (cell_x + row)*MATRIX_DEEP + cell_y + CELL_DEEP; j++) {
                    memcpy(&sub_list[k++], &node_db[j], sizeof(NODE_INFO_ST));
                }
            }        
    }

    return 0;        
}

int restore_sub_node_db(int mode, NODE_INFO_ST *node_db, int i, NODE_INFO_ST* sub_list)
{
    int j, k, row;
    int cell_x, cell_y;
    
    assert(node_db != NULL);
    assert(sub_list != NULL);

    if(mode == 1) // row(i)
    {
        for (j = i*MATRIX_DEEP, k = 0; j < i*MATRIX_DEEP + MATRIX_DEEP; j++) {
            memcpy(&node_db[j], &sub_list[k++], sizeof(NODE_INFO_ST));
        }    
    }
    else if (mode == 2) // col(i)
    {
        for (j = i, k = 0; j < MATRIX_NUM; j+= MATRIX_DEEP) {
            memcpy(&node_db[j], &sub_list[k++], sizeof(NODE_INFO_ST));
        }
    }
    else if (mode == 3) // cell(i)
    {
        cell_x = (i/CELL_DEEP)*CELL_DEEP;
        cell_y = (i%CELL_DEEP)*CELL_DEEP;
        for(row = 0, k = 0; row < CELL_DEEP; row++) {
                for (j = (cell_x + row)*MATRIX_DEEP + cell_y; j < (cell_x + row)*MATRIX_DEEP + cell_y + CELL_DEEP; j++) {
                    memcpy(&node_db[j], &sub_list[k++], sizeof(NODE_INFO_ST));
                }
            }        
    }

    return 0;        
}
#endif

#if 1 // try 
int check_sub_node_db(NODE_INFO_ST* sub_list)
{
    int i, j;
    
    assert(sub_list != NULL);
    
    // 检查是否存在节点取值冲突
    for(i = 0; i < MATRIX_DEEP; i++) {
        for (j = 0; j < MATRIX_DEEP; j++) {
            if(j != i && sub_list[i].value > 0 && sub_list[i].value == sub_list[j].value) 
                return 1;
        }
    }

    return 0;
}

int check_node_db(NODE_INFO_ST *node_db)
{
    int i, ret = 0;
    NODE_INFO_ST sub_node_db[MATRIX_DEEP];
    
    assert(node_db != NULL);
    
    for(i = 0; i < MATRIX_DEEP; i++) 
    {
        load_sub_node_db(1, node_db, i, sub_node_db);
        ret = check_sub_node_db(sub_node_db);
        restore_sub_node_db(1, node_db, i, sub_node_db);
        if (ret != 0) return ret;
    }

    for(i = 0; i < MATRIX_DEEP; i++) 
    {
        load_sub_node_db(2, node_db, i, sub_node_db);
        ret = check_sub_node_db(sub_node_db);
        restore_sub_node_db(2, node_db, i, sub_node_db);
        if (ret != 0) return ret;
    }

    for(i = 0; i < MATRIX_DEEP; i++) 
    {
        load_sub_node_db(3, node_db, i, sub_node_db);
        ret = check_sub_node_db(sub_node_db);
        restore_sub_node_db(3, node_db, i, sub_node_db);
        if (ret != 0) return ret;
    }    

    return 0;
}


int try_node_db(int index, NODE_INFO_ST *node_db)
{
    int i;
    int bitmap_bak;
    NODE_INFO_ST new_node_db[MATRIX_NUM];
    
    assert(node_db != NULL);
    if (index >= MATRIX_NUM) return 0;
    
    if (node_db[index].value > 0) 
        return try_node_db(index + 1, node_db);

    bitmap_bak = node_db[index].bitmap;
    for(i = 0; i < MATRIX_DEEP; i++)
    {
        if ( TEST_BITMAP(bitmap_bak, i) )
        {
            //printf("try node %d with value %d \n", index + 1, i + 1);
            memcpy(new_node_db, node_db, MATRIX_NUM*sizeof(NODE_INFO_ST));
            new_node_db[index].bitmap = 1 << i;
            new_node_db[index].value = i + 1;

            //check if conflict
            if ( check_node_db(new_node_db) > 0 ) 
            {
                continue; //not good
            } 
            else
            {
                if (get_all_checked_num(new_node_db) == MATRIX_NUM) {
                    printf("try success\n");
                    memcpy(my_node_db, new_node_db, MATRIX_NUM*sizeof(NODE_INFO_ST));
                    return 0;
                }

                if ( 0 == try_node_db(index + 1, new_node_db) )
                    return 0;
            }
        }
    }
    
    return 1;
}


#endif

int main()
{
    int i,j;
    int temp[MATRIX_DEEP];
    
    // get init data
    get_init_data();
    show_init_data(my_init_data);

    // generate node db
    init_node_db(my_node_db, my_init_data);
    show_node_db(__LINE__, my_node_db);

#if 0
    // scan 
    printf("scan...\n");
    scan_node_db_rep(my_node_db);
    show_node_db(__LINE__, my_node_db);
    if (get_all_checked_num(my_node_db) == MATRIX_NUM) 
        goto PASS_CHECK;
#endif

    // try
    printf("try...\n");
    try_node_db(0, my_node_db);
    show_node_db(__LINE__, my_node_db);

PASS_CHECK:
    if (check_node_db(my_node_db) == 0)  printf("DONE.\n"); 
    else printf("ERROR.\n");
    
    return 0;
}

 

目录
相关文章
|
2月前
|
算法
LeetCode回文数(暴力解,求更好的思路)
这篇博客讨论了如何判断一个整数是否为回文数,提供了暴力解法的代码,并寻求更优的算法建议。
43 1
LeetCode回文数(暴力解,求更好的思路)
|
6月前
|
存储 SQL 算法
LeetCode 题目 87:递归\动态规划\哈希实现 扰乱字符串
LeetCode 题目 87:递归\动态规划\哈希实现 扰乱字符串
|
算法
LeetCode 37 解数独 循环+回溯算法
LeetCode 37 解数独 循环+回溯算法
59 0
|
算法 Java
【算法题目解析】杨氏矩阵数字查找
一道面试时可能遇到的算法问题,杨氏矩阵。可以重点关注思考方式,而不是死记硬背。
41 0
剑指offer_递归与循环---矩形覆盖
剑指offer_递归与循环---矩形覆盖
81 0
剑指offer_递归与循环---扑克牌顺子
剑指offer_递归与循环---扑克牌顺子
51 0
字符串的逆序(循环和递归两种解法)
字符串的逆序(循环和递归两种解法)
178 0
|
机器学习/深度学习 算法
do while循环以及一些题目
do while循环以及一些题目
121 0
复习C部分:1.for循环 2.do while循环语句 3.在一个有序数组中查找某个数,例如在1~10之间找7(例题包含计算n的阶乘+打印1~10的奇数+二分法)
复习C部分:1.for循环 2.do while循环语句 3.在一个有序数组中查找某个数,例如在1~10之间找7(例题包含计算n的阶乘+打印1~10的奇数+二分法)
117 0
复习C部分:1.for循环 2.do while循环语句 3.在一个有序数组中查找某个数,例如在1~10之间找7(例题包含计算n的阶乘+打印1~10的奇数+二分法)
递归题目练习---数独问题
递归题目练习---数独问题
111 0
递归题目练习---数独问题