• 数独生成算法


    算法:

    1. 随机填充中间的九宫格

    2. 通过交换行、列,填充中间的九宫格的上、下、左、右四个九宫格

    3. 同样方式填充四个角上的九宫格

    4. 根据难度随机挖去数

    C++源代码如下:

    #include <ctime>
    #include <cstring>
    #include <iostream>
    #include <vector>
    #include <algorithm>
     
    using namespace std;
     
    const int N = 9;
    vector<int> src;
    int table[N][N];
     
    void ouput()
    {
        for (int i = 0; i < 9; ++i)
        {
            for (int j = 0; j < 9; ++j)
            {
                cout << table[i][j] << "  ";
            }
            cout << endl;
        }
    }
     
    // 初始化中间的九宫格
    void centerInit()
    {
        for (int i = 0; i < N; ++i)
            src.push_back(i + 1);
        random_shuffle(src.begin(), src.end()); // 随机化序列
     
        int k = 0;
        for (int i = 3; i < 6; ++i)
            for (int j = 3; j < 6; ++j)
                table[i][j] = src[k++];
    }
     
    // 由中间的九宫格交叉变换,初始化上下左右四个九宫格
    void crossInit()
    {
        for (int i = 3; i < 6; ++i)
        {
            int l = 0;
            for (int j = 3; j < 6; ++j)
            {
                if (i == 3)
                {
                    table[i + 1][l] = table[i][j];
                    table[i + 2][l + 6] = table[i][j];
                    ++l;
                }
                else if (i == 4)
                {
                    table[i + 1][l] = table[i][j];
                    table[i - 1][l + 6] = table[i][j];
                    ++l;
                }
                else if (i == 5)
                {
                    table[i - 2][l] = table[i][j];
                    table[i - 1][l + 6] = table[i][j];
                    ++l;
                }
            }
        }
        for (int j = 3; j < 6; ++j)
        {
            int l = 0;
            for (int i = 3; i < 6; ++i)
            {
                if (j == 3)
                {
                    table[l][j + 1] = table[i][j];
                    table[l + 6][j + 2] = table[i][j];
                    ++l;
                }
                else if (j == 4)
                {
                    table[l][j + 1] = table[i][j];
                    table[l + 6][j - 1] = table[i][j];
                    ++l;
                }
                else if (j == 5)
                {
                    table[l][j - 2] = table[i][j];
                    table[l + 6][j - 1] = table[i][j];
                    ++l;
                }
            }
        }
    }
     
    // 初始化四个角上的四个九宫格
    void cornerInit()
    {
        for (int i = 0; i < 3; ++i)
        {
            int l = 0;
            for (int j = 3; j < 6; ++j)
            {
                if (i == 0)
                {
                    table[i + 1][l] = table[i][j];
                    table[i + 2][l + 6] = table[i][j];
                    ++l;
                }
                else if (i == 1)
                {
                    table[i + 1][l] = table[i][j];
                    table[i - 1][l + 6] = table[i][j];
                    ++l;
                }
                else if (i == 2)
                {
                    table[i - 2][l] = table[i][j];
                    table[i - 1][l + 6] = table[i][j];
                    ++l;
                }
            }
        }
        for (int i = 6; i < 9; ++i)
        {
            int l = 0;
            for (int j = 3; j < 6; ++j)
            {
                if (i == 6)
                {
                    table[i + 1][l] = table[i][j];
                    table[i + 2][l + 6] = table[i][j];
                    ++l;
                }
                else if (i == 7)
                {
                    table[i + 1][l] = table[i][j];
                    table[i - 1][l + 6] = table[i][j];
                    ++l;
                }
                else if (i == 8)
                {
                    table[i - 2][l] = table[i][j];
                    table[i - 1][l + 6] = table[i][j];
                    ++l;
                }
            }
        }
    }
     
    // 根据设定的难度随机挖数
    void generateSudoku(int difficulty)
    {
        difficulty *= 20;
        while (difficulty--)
        {
            table[rand() % 9][rand() % 9] = 0;
        }
    }
     
    // 初始化数独
    // 先随机生成中间的九宫格,再通过交叉生成上下左右四个九宫格,
    // 最后通过交叉生成四个角上的九宫格 /* 交叉顺序如下: 9 7 8 3 1 2 6 4 5 3 1 2 6 4 5 9 7 8 6 4 5 9 7 8 3 1 2 7 8 9 1 2 3 4 5 6 1 2 3 4 5 6 7 8 9 4 5 6 7 8 9 1 2 3 8 9 7 2 3 1 5 6 4 2 3 1 5 6 4 8 9 7 5 6 4 8 9 7 2 3 1 */ void init(int difficulty) { srand(time(NULL)); memset(table, 0, sizeof(table)); centerInit(); crossInit(); cornerInit(); generateSudoku(difficulty); } int main() { int d; while (true) { cout << "Please select the difficulty(1~4), input 0 to exit: "; cin >> d; if (d == 0) { break; } init(d); ouput(); } return 0; }
  • 相关阅读:
    【小白学PyTorch】1 搭建一个超简单的网络
    【小白学PyTorch】2 浅谈训练集和测试集
    【小白学AI】GBDT梯度提升详解
    【小白学AI】XGBoost推导详解与牛顿法
    【小白写论文】技术性论文结构剖析
    小白学PyTorch 动态图与静态图的浅显理解
    【小白学推荐1】 协同过滤 零基础到入门
    【小白学AI】随机森林 全解 (从bagging到variance)
    OpenCV开发笔记(七十二):红胖子8分钟带你使用opencv+dnn+tensorFlow识别物体
    【python刷题】二叉搜索树-相关题目
  • 原文地址:https://www.cnblogs.com/qwertWZ/p/2669144.html
Copyright © 2020-2023  润新知