C 数独求解程序

标签 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 

最佳答案

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

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

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

关于C 数独求解程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35202041/

相关文章:

java - 生成数独网格时出现意外输出

vba - 将收敛值设置为内置求解器的变量

c# - 改进我的扫雷算法 C#

c - 系统如何确保内存和缓存之间的一致性

c++ - 如何在《糖果粉碎传奇》等游戏中为图 block 分配颜色

python - 我需要在此数独 python 代码中进行解释

Excel 解算器具有非相邻单元格约束?

c - 理解传递的这个参数

c - 如何在 C 语言的 printf 输出中使用除法的模运算符显示余数

javascript - 如何动态查找数独子框值