C数独解题程序

C Sudoku solving program

我正在尝试编写一个解决数独的简单程序。它编译,但不工作。我怀疑(通过调试)函数 move() 会导致问题,但我无法准确识别错误。程序是循环的,虽然它确实从文件或键盘输入加载数独,但似乎用零填充的数组被传递给函数 SolveSudoku - 这是我的猜测。你能看看我的代码吗?提前致谢!

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

int sudoku[9][9];
int SolveSudoku(int, int);

/* Function checks, if in given row the number already exists */
int CheckRow(int row, int liczba)
{
    int column;
    for (column = 0; column < 9; column++)
    {
        if (sudoku[row][column] == liczba)
            return 0;
    }
    return 1;
}

/* Function checks, if in given column the number already exists */
int CheckColumn(int column, int liczba)
{
    int row;
    for (row = 0; row < 9; row++)
    {
        if (sudoku[row][column] == liczba)
            return 0;
    }
    return 1;
}

/* Function checks, if in given grid the number already exists */
int CheckGrid(int row, int column, int liczba)
{
    row = (row/3)*3;
    column = (column/3)*3;
    int i = 0;
    int j = 0;
    for (i = 0; i < 3; i++)
    {
        for (j = 0; j < 3; j++)
        {
            if (sudoku[row + i][column + j] == liczba)
                return 0;
        }
    }
    return 1;
}

/* If grid is filled, function moves to another grid */

void Move(int row, int column)
{
    if (column < 8)
        SolveSudoku(row, column + 1);
    else
        SolveSudoku(row + 1, 0);
}

int Print(int array[9][9])
{
    int row, column;

    for (row = 0; row < 9; row++)
    {
        if ( row == 3 || row == 6)
        {
            printf("=======================\n");
        }
        for (column = 0; column < 9; column++)
        {
            printf("%d ", array[row][column]);
            if (column == 2 || column == 5)
            {
                printf("|| ");
            }
        }
        printf("\n");
    }
        return(0);
}

int SolveSudoku(int row, int column)
{
/* If row > 8, then Sudoku is solved */
    if (row > 8)
    {
        Print(sudoku);
        return 0;
    }
/* If in given grid there is already a number different from 0, we move */
    if (sudoku[row][column] != 0)
    {
        Move(row, column);
    }
/* If there is 0, it is filled with number and then move */
    else
    {
        int licznik;
        for (licznik = 1; licznik <= 9; licznik++)
        {
            if ((CheckRow(row, licznik) == 1) && (CheckColumn(column, licznik) == 1) && (CheckGrid(row, column, licznik) == 1))
            {
                sudoku[row][column] = licznik;
                Move(row, column);
            }
        }
        sudoku[row][column] = 0;
    }
    return 0;
}

int main()
{
    int sudoku[9][9];
    int row, column;
    int option;
    char file[100];
    printf("This is Sudoku Solver.\nChoose one of the option:\n");
    printf("1. Enter Sudoku from keyboard\n2. Load Sudoku from file\n3. Exit program\n");
    scanf("%d", &option);

    switch (option)
    {
        case 1:
            printf("Enter Sudoku. For blank spaces enter 0. Separate numbers with space.\n");
            for (row = 0; row < 9; row++)
            {
                for (column = 0; column < 9; column++)
                {
                    scanf("%d", &sudoku[row][column]);
                }
            } 
            printf("Loaded Sudoku:\n");
            Print(sudoku);
            break;
        case 2:
            printf("Enter filename.\nFilename: ");
            scanf("%99s", file);
            FILE *fp = fopen ( file, "r" );
            if ( fp == NULL )
            {
                printf("Couldnt open fil: %s.\nAre you sure that the program and file are in the same folder?\n\n", file);
                exit(EXIT_FAILURE);
            }

            while (!feof(fp))
            {

                for (row = 0; row < 9; row++)
                {
                    for (column = 0; column < 9; column++)
                    {
                        fscanf(fp, "%d ", &sudoku[row][column]);
                    }
                }

            }
            fclose(fp);
            printf("Loaded Sudoku:\n");
            Print(sudoku);
            break;

        case 3:
            exit(0); break;

        default:
            printf("\nOption not recognized. Exit.\n"); break;
    }
    printf("\nDo you want to solve displayed Sudoku? Y \ N");
    char askSolve;
    scanf("%s", &askSolve);
    switch (askSolve)
    {
        case 'T': 
            printf("\nSolved Sudoku:\n");
            SolveSudoku(0,0);
            break;
        case 'N':
            printf("Goodbye\n");
            exit(0);
            break;
        default:
            printf("Option unknown. Goodbye.\n");
            break;
    }

    return 0;
}

此外,一些随机数独输入:

9 0 0 2 3 7 6 8 0
0 2 0 8 4 0 0 7 3
8 0 7 1 0 5 0 2 9
0 0 4 5 9 8 3 0 0 
2 0 0 0 0 1 0 0 6
5 1 0 0 0 0 0 4 7
4 0 1 3 0 6 2 9 5
0 5 0 9 1 0 7 3 8
3 0 8 0 5 0 0 0 0 

您正在将数据读入 main 的局部数组 int sudoku[9][9];,其中求解器使用全局数组 int sudoku[9][9];

main中删除数组,一切都会好起来的。

--编辑:我看到MikeCAT在他的评论中说了这个。--