简体   繁体   English

在 C 上制作一个 empy 数独求解器

[英]Making an empy Sudoku solver on C

So i got an assignment in class to make an empty sudoku that every time creates a random solution of 9x9.所以我在 class 中得到了一个任务来制作一个空数独,每次都会创建一个 9x9 的随机解。 I got to the point where i get different number each row and column but not on every 3x3 matrix and i cannot figure out how to go on from here.我到了每行和每列得到不同数字但不是在每个 3x3 矩阵上得到不同数字的地步,我无法弄清楚如何从这里开始 go。 We didnt learn recursion yet and can use only the libraries listed in the code.我们还没有学习递归,只能使用代码中列出的库。

  #include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NINE 9
#define ONE 1

void solve_sudoku(int board[9][9])
{
    srand(time(0));
    int count = 0;
    for (int i = 0;i <= NINE;i++)
    {
        for (int j = 0;j < NINE;j++)
        {
            board[i][j] =(rand() % NINE)+ONE;
            for (int k = 0;k < 9;k++)
            {
                int clone_i = i;
                int clone_j = j;
                while (board[i][k] == board[i][j])
                {
                    if (j == k)
                    {
                        break;
                    }
                    count++;
                    board[i][j] = (rand() % NINE) + ONE;
                    k = 0;
                }
                while(board[k][j]==board[i][j])
                    {
                    if (i == k)
                    {
                        break;
                    }
                    count++;
                        board[i][j] = (rand() % NINE) + ONE;
                        k = 0;
                    }
                if (count > 300 || (board[i][j] == board[i][k] && j != k))
                {
                    for (int i = clone_i;i < clone_i + 1;i++)
                        for (int l = 0;l < 9;l++)
                        {
                            board[i][l] = 0;
                        }
                    count = 0;
                    k = 0;
                    j = 0;
                }
                
                }
            }
        }
    }



void print_sudoku(int board[][9])
{
    printf("The soduko solution is: \n");
    for (int i = 0;i < NINE;i++)
    {
        for (int k = 0;k < NINE;k++)
        {
            printf("%d ", board[i][k]);
        }
        printf("\n");
    }
}

int main()
{
    int sud[9][9] = { 0 };
    int matrix_size = 9;
    solve_sudoku(sud);
    print_sudoku(sud);
    return 0;
}

I take you to mean that you need to generate random 9 x 9 grids of digits that meet the Sudoku criterion that each row, column and block contains all nine digits.我认为您的意思是您需要生成符合数独标准的随机 9 x 9 数字网格,即每行、列和块都包含所有九位数字。 In that case, you are going about it a very difficult way.在这种情况下,您将采取非常困难的方式。 Perhaps that was inspired by viewing the program as a solver, instead of what it really needs to be: a generator.也许这受到了将程序视为求解器的启发,而不是它真正需要的东西:生成器。

Consider that it is easy to write down at least one valid Sudoku algorithmically:考虑到用算法写下至少一个有效的数独是很容易的:

 1 2 3 | 4 5 6 | 7 8 9
 4 5 6 | 7 8 9 | 1 2 3
 7 8 9 | 1 2 3 | 4 5 6
 ------+-------+------
 2 3 4 | 5 6 7 | 8 9 1
 5 6 7 | 8 9 1 | 2 3 4
 8 9 1 | 2 3 4 | 5 6 7
 ------+-------+------
 3 4 5 | 6 7 8 | 9 1 2
 6 7 8 | 9 1 2 | 3 4 5
 9 1 2 | 3 4 5 | 6 7 8

Now consider that you can always transform one valid Sudoku into a different one by swapping two rows or two columns such that no entries move from one block to another.现在考虑您总是可以通过交换两行或两列将一个有效的数独转换为不同的数独,这样没有条目从一个块移动到另一个块。 For example, you can swap the first row with the third, or the fifth column with the sixth.例如,您可以将第一行与第三行交换,或将第五列与第六列交换。 If you perform a bunch of random swaps of that kind on a valid starting Sudoku then you will end up with a random grid that meets the Sudoku criteria.如果你在一个有效的起始数独上执行一堆这种随机交换,那么你最终会得到一个满足数独标准的随机网格。

Note that it is a different story if you need to produce only Sudoku that can be solved by deduction alone, without trial & error.请注意,如果您只需要制作可以仅通过推理解决的数独,而无需反复试验,那就是另一回事了。 For that you probably do need a solver-based approach, but that starts with a bona fide solver, and nothing in your code is anything like that.为此,您可能确实需要一种基于求解器的方法,但从真正的求解器开始,您的代码中没有任何类似的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM