简体   繁体   中英

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. 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
6 XX 1 9 5 XXX
X 9 8 XXXX 6 X
8 XXX 6 XXX 3
4 XX 8 X 3 XX 1
7 XXX 2 XXX 6
X 6 XXXX 2 8 X
XXX 4 1 9 XX 5
XXXX 8 XX 7 9

expected output:

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

what I end up getting:

5 3 1 | 2 7 6 | 4 9 8
6 2 4 | 1 9 5 | 7 3 0
0 9 8 | 0 0 0 | 0 6 0
- - - - - - - - - - -
8 0 0 | 0 6 0 | 0 0 3
4 0 0 | 8 0 3 | 0 0 1
7 0 0 | 0 2 0 | 0 0 6
- - - - - - - - - - -
0 6 0 | 0 0 0 | 2 8 0
0 0 0 | 4 1 9 | 0 0 5
0 0 0 | 0 8 0 | 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. 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.

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.

If the number is fixed, shouldn't you just consider that as a valid solution and return 0 instead?

I don't see any code in your function that sets sudoku ->value to zero.

[edit] But when column overflows, your function returns 0, without finding a solution. The zeroes we see are uninitialized values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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