简体   繁体   中英

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. 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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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