我正在尝试编写一个解决Sudoku的简单程序。它可以编译,但是不起作用。我怀疑(通过调试)函数move()会导致问题,但我无法准确识别该错误。程序已循环运行,尽管它确实从文件或键盘输入中加载了Sudoku,但似乎填充零的数组将传递给函数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

最佳答案

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

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

--edit:我看到MikeCAT在他的评论中这样说。

10-08 03:53