C数独解决方案

时间:2016-02-04 13:17:20

标签: c sudoku solver

我正在尝试编写一个解决数独的简单程序。它编译,但不起作用。我怀疑(从调试)函数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 

1 个答案:

答案 0 :(得分:1)

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

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

- 编辑:我看到MikeCAT在评论中这样说.--