简体   繁体   English

C Sudoku求解器通过回溯卡住

[英]C Sudoku solver by backtracking getting stuck

I'm trying to use backtracking to code a sudoku solver in C, but it is getting stuck at some point. 我正在尝试使用回溯在C语言中编写数独求解器,但是它在某些时候陷入了困境。 All the other functions are working well, so I can't seem to find the problem. 所有其他功能都运行良好,因此我似乎找不到问题。 The function was supposed to call itself using the next number as a parameter. 该函数应该使用下一个数字作为参数来调用自身。 returning one of the value is valid and zero when it Isn't. 返回值之一是有效的,如果不是则返回零。 This in turn was supposed to trigger the backtracking. 这反过来又会触发回溯。 It should work like this until there are no more lines left. 它应该像这样工作,直到没有更多的行了。

input: 输入:

5 3 XX 7 XXXX 5 3 XX 7 XXXX
6 XX 1 9 5 XXX 6 XX 1 9 5 XXX
X 9 8 XXXX 6 X X 9 8 XXXX 6 X
8 XXX 6 XXX 3 8 XXX 6 XXX 3
4 XX 8 X 3 XX 1 4 XX 8 X 3 XX 1
7 XXX 2 XXX 6 7 XXX 2 XXX 6
X 6 XXXX 2 8 X X 6 XXXX 2 8 X
XXX 4 1 9 XX 5 XXX 4 1 9 XX 5
XXXX 8 XX 7 9 XXXX 8 XX 7 9

expected output: 预期输出:

5 3 4 | 5 3 4 | 6 7 8 | 6 7 8 | 9 1 2 9 1 2
6 7 2 | 6 7 2 | 1 9 5 | 1 9 5 | 3 4 8 3 4 8
1 9 8 | 1 9 8 | 3 4 2 | 3 4 2 | 5 6 7 5 6 7
- - - - - - - - - - - ------------
8 5 9 | 8 5 9 | 7 6 1 | 7 6 1 | 4 2 3 4 2 3
4 2 6 | 4 2 6 | 8 5 3 | 8 5 3 | 7 9 1 7 9 1
7 1 3 | 7 1 3 | 9 2 4 | 9 2 4 | 8 5 6 8 5 6
- - - - - - - - - - - ------------
9 6 1 | 9 6 1 | 5 3 7 | 5 3 7 | 2 8 4 2 8 4
2 8 7 | 2 8 7 | 4 1 9 | 4 1 9 | 6 3 5 6 3 5
3 4 5 | 3 4 5 | 2 8 6 | 2 8 6 | 1 7 9 1 7 9

what I end up getting: 我最终得到的是:

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

Here is the backtracking function: 这是回溯功能:

int solve(slot sudoku[9][9],int line, int column){
    int num=1;  
    while(sudoku[line][column].fix==1){
        column++;
    }
    if(line==9){
        return 1;
    }
    for(num=1;num<10;num++){
        if(Valid(line,column,num)){
            sudoku[line][column].value=num;
            if(column<8){
                if(solve(sudoku,line,column+1)){
                    return 1;
                }
            }
            else if(column==8){
                if(solve(sudoku,line+1,0)){
                    return 1;
                }
            }
        }
    }
    return 0;


}

This is how I defined the "slot" struct. 这就是我定义“ slot”结构的方式。 It's purpose was to "pin" the original values of the puzzle, so it wouldn't overwrite them. 目的是“固定”拼图的原始值,因此不会覆盖它们。 It worked by assigning "0" to the slots that could be altered by the function, and "1" to the slots that were to remain fixed. 它通过为功能可以更改的插槽分配“ 0”,并为要固定的插槽分配“ 1”来工作。

typedef struct slot{
    int value;
    int fix;
}slot;

If someone could help me find the problem, I would be extremely grateful. 如果有人可以帮助我发现问题,我将非常感谢。

What do you do in or after this loop, when column overflows your array size? 当列溢出数组大小时,您在循环中或循环后做什么?

  while(sudoku[line][column].fix==1)
  {
        column++;
  }

When column overflows, the rest of the function uses that value, it's passed as a parameter to Valid() , among other things. 当列溢出时,函数的其余部分将使用该值,并将其作为参数传递给Valid()等。

If the number is fixed, shouldn't you just consider that as a valid solution and return 0 instead? 如果数字是固定的,您是否不应该仅将其视为有效的解决方案,而是返回0?

I don't see any code in your function that sets sudoku ->value to zero. 我看不到您的函数中将sudoku > value设置为零的任何代码。

[edit] But when column overflows, your function returns 0, without finding a solution. [edit]但是,当列溢出时,您的函数将返回0,而没有找到解决方案。 The zeroes we see are uninitialized values. 我们看到的零是未初始化的值。

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

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