使用C++生成一个数独矩阵

数独矩阵即满足如下要求的矩阵:阶为9的方阵,其每行、每列、将其分为9宫后的每一个3*3的方阵,都包含1-9九个数字各一次。

本段代码所用的算法为:

1)设定一个初始矩阵matrix,该矩阵是一个满足条件的数独矩阵

2)因为每一个满足条件的矩阵,其0-2、3-5、6-8行与0-2、3-5、6-8列间相互打乱顺序,仍然可以得到一个满足条件的矩阵,可以通过随机数打乱它们的顺序

3)因为将一个满足条件的矩阵中任取两个不同的元素,相互替换后仍然可以得到一个满足条件的矩阵(如值A全部换为值B,同时值B全部换为值A),可以通过随机数进行替换

4)最后得到的矩阵即为所求

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    //Set the seed of random numbers
    srand((unsigned)time(0));

    //Rule:
    //1.Each block has 9 numbers 1-9
    //2.Each row has 9 numbers 1-9
    //3.Each column has 9 numbers 1-9
    
    int matrix[9][9] = 
    {
        {1, 2, 3,  7, 8, 9,  4, 5, 6},
        {4, 5, 6,  1, 2, 3,  7, 8, 9},
        {7, 8, 9,  4, 5, 6,  1, 2, 3},
        
        {3, 1, 2,  9, 7, 8,  6, 4, 5},
        {6, 4, 5,  3, 1, 2,  9, 7, 8},
        {9, 7, 8,  6, 4, 5,  3, 1, 2},
        
        {2, 3, 1,  8, 9, 7,  5, 6, 4},
        {5, 6, 4,  2, 3, 1,  8, 9, 7},
        {8, 9, 7,  5, 6, 4,  2, 3, 1}
    };
    
    //1.Change rows and columns
    int counter = 30;
    while(--counter)
    {
        int i = rand() % 3;  //big row no.
        int j = rand() % 3;  //big column no.
        int k = rand() % 2;  //0:horizontally 1:vertically
        
        //0:123 1:132 2:213 3:231 4:312 5:321
        switch(rand() % 6)
        {
            case 0: break; //123 
            case 1: //132
                {
                    if(k == 0)
                    {
                        int temp;
                        for(int counter = 0; counter < 9; counter++)
                        {
                            temp = matrix[i * 3 + 1][counter];
                            matrix[i * 3 + 1][counter] = matrix[i * 3 + 2][counter];
                            matrix[i * 3 + 2][counter] = temp;
                        }
                    }
                    else
                    {
                        int temp;
                        for(int counter = 0; counter < 9; counter++)
                        {
                            temp = matrix[counter][j * 3 + 1];
                            matrix[counter][j * 3 + 1] = matrix[counter][j * 3 + 2];
                            matrix[counter][j * 3 + 2] = temp;
                        }
                    }
                }
                break;
            case 2: //213
                {
                    if(k == 0)
                    {
                        int temp;
                        for(int counter = 0; counter < 9; counter++)
                        {
                            temp = matrix[i * 3 + 0][counter];
                            matrix[i * 3 + 0][counter] = matrix[i * 3 + 1][counter];
                            matrix[i * 3 + 1][counter] = temp;
                        }
                    }
                    else
                    {
                        int temp;
                        for(int counter = 0; counter < 9; counter++)
                        {
                            temp = matrix[counter][j * 3 + 0];
                            matrix[counter][j * 3 + 0] = matrix[counter][j * 3 + 1];
                            matrix[counter][j * 3 + 1] = temp;
                        }
                    }
                }
                break;
            case 3: //231
                {
                    if(k == 0)
                    {
                        int temp;
                        for(int counter = 0; counter < 9; counter++)
                        {
                            temp = matrix[i * 3 + 0][counter];
                            matrix[i * 3 + 0][counter] = matrix[i * 3 + 1][counter];
                            matrix[i * 3 + 1][counter] = matrix[i * 3 + 2][counter];
                            matrix[i * 3 + 2][counter] = temp;
                        }
                    }
                    else
                    {
                        int temp;
                        for(int counter = 0; counter < 9; counter++)
                        {
                            temp = matrix[counter][j * 3 + 0];
                            matrix[counter][j * 3 + 0] = matrix[counter][j * 3 + 1];
                            matrix[counter][j * 3 + 1] = matrix[counter][j * 3 + 2];
                            matrix[counter][j * 3 + 2] = temp;
                        }
                    }
                }
                break;
            case 4: //312
                {
                    if(k == 0)
                    {
                        int temp;
                        for(int counter = 0; counter < 9; counter++)
                        {
                            temp = matrix[i * 3 + 2][counter];
                            matrix[i * 3 + 2][counter] = matrix[i * 3 + 1][counter];
                            matrix[i * 3 + 1][counter] = matrix[i * 3 + 0][counter];
                            matrix[i * 3 + 0][counter] = temp;
                        }
                    }
                    else
                    {
                        int temp;
                        for(int counter = 0; counter < 9; counter++)
                        {
                            temp = matrix[counter][j * 3 + 2];
                            matrix[counter][j * 3 + 2] = matrix[counter][j * 3 + 1];
                            matrix[counter][j * 3 + 1] = matrix[counter][j * 3 + 0];
                            matrix[counter][j * 3 + 0] = temp;
                        }
                    }
                }
                break;
            case 5: //321
                {
                    if(k == 0)
                    {
                        int temp;
                        for(int counter = 0; counter < 9; counter++)
                        {
                            temp = matrix[i * 3 + 0][counter];
                            matrix[i * 3 + 0][counter] = matrix[i * 3 + 2][counter];
                            matrix[i * 3 + 2][counter] = temp;
                        }
                    }
                    else
                    {
                        int temp;
                        for(int counter = 0; counter < 9; counter++)
                        {
                            temp = matrix[counter][j * 3 + 0];
                            matrix[counter][j * 3 + 0] = matrix[counter][j * 3 + 2];
                            matrix[counter][j * 3 + 2] = temp;
                        }
                    }
                }
                break;
        }
    }

    //2.Change the numbers
    counter = 10;
    while(--counter)
    {
        int tempa = rand() % 9 + 1;
        int tempb;
        do
        {
            tempb = rand() % 9 + 1;
        }while(tempb == tempa);

        for(int i = 0; i < 9; i++)
        {
            for(int j = 0; j < 9; j++)
            {
                if(matrix[i][j] == tempa)
                {
                    matrix[i][j] = tempb;
                }
                else if(matrix[i][j] == tempb)
                {
                    matrix[i][j] = tempa;
                }
            }
        }
    }

    //Print the matrix
    for(int i = 0; i < 9; i++)
    {
        for(int j = 0; j < 9; j++)
        {
            cout << matrix[i][j] << " ";
        }
        cout << endl;
    }

    return 0;
}

END

转载于:https://my.oschina.net/Tsybius2014/blog/271188

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值