简体   繁体   English

递归回溯数独求解器

[英]Recursive backtracking Sudoku solver

I'm currently learning C , mainly with examples from Project Euler. 我目前正在学习C ,主要是来自Euler项目的示例。 (<- check it out if you don't already know it!) (<-如果您还不知道,请检查一下!)

Out of some sources on the internet and some self write code, I made a C program which should solve 50 Sudokus. 根据互联网上的一些资源和一些自行编写的代码,我制作了一个可以解决50个Sudokus的C程序。 I've defined an array like [number of sudoku][row][column]. 我定义了一个数组,如[数独数] [行] [列]。 The following is the solving function. 以下是求解函数。

int solve(int puzzles[50][9][9], int sudokuNumber, int row, int col){
int nextNum = 1;

if (row == 8){
    return 1;
}

if (puzzles[sudokuNumber][row][col]){
    if (col == 8){
        if (solve(puzzles, sudokuNumber, row+1, 0))
            return 1;
    }
    else {
        if (solve(puzzles, sudokuNumber, row, col+1))
            return 1;
    }
    return 0;
}

for (; nextNum <= 9; nextNum++){
    if (isValid(sudokuNumber, row, col, nextNum)){
        puzzles[sudokuNumber][row][col] = nextNum;
        if (col == 8){
            if (solve(puzzles, sudokuNumber, row+1, 0))
                return 1;
        }
        else {
            if (solve(puzzles, sudokuNumber, row, col+1))
                return 1;
        }
    }
}
}

Which unfortunately doesn't really output anything and at this point and is quite frustrating... Am I making an obvious dumb mistake? 不幸的是,哪一个并没有真正输出任何东西,并且现在令人沮丧……我犯了一个明显的愚蠢错误吗? isValid returns 1 if valid, this function is correct. 如果有效,则isValid返回1,此函数正确。

Would really appreciate if someone would have a look at this. 如果有人来看看,将不胜感激。

First of all, I think the function signatures need to be fixed. 首先,我认为功能签名需要固定。 They should take one board and not a list of boards. 他们应该选一个董事会,而不是董事会清单。 Iterate on the list outside of the solve function (in fact there is no need to load all the boards into memory, you only need to sum the numbers formed by (0,0) (0,1), (0,2) position from all boards) 在求解函数之外的列表上进行迭代(实际上,无需将所有电路板都加载到内存中,您只需要对由(0,0)(0,1),(0,2)位置形成的数字求和从所有董事会)
void solve(int board[][], int row, int col) -> Try solving starting at board[row][col] void solve(int board[][], int row, int col) ->尝试从board [row] [col]开始求解
bool is_valid(int board[][], int row, int col, int d) -> is it ok to fill board[row][col] with d bool is_valid(int board[][], int row, int col, int d) ->是否可以用d填充board [row] [col]

Second, the boundary condition seems to be not correct. 第二,边界条件似乎不正确。 Assuming row & col take on values 0 through 8, it should be row==9 and col==9 instead of 8. 假设row&col的取值范围为0到8,则应为row==9col==9而不是8。

Also, I think you'll have to skip the already filled cells. 另外,我认为您必须跳过已经填充的单元格。 if(board[row][col] != 0) solve(board, row, col+1)

Also, depending on how is_valid is implemented, you'll have to make sure you're un-filling properly. 另外,根据is_valid的实现方式,您必须确保正确填充。 In the problem, unfilled rows are represented as 0s. 在问题中,未填充的行表示为0。 So before you backtrack after failing to fill board[row][col] with all numbers from 1 to 9, make sure to set board[row][col] back to 0. 因此,在未能以1到9的所有数字填充board [row] [col]后回溯之前,请确保将board [row] [col]设置回0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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