I'm currently learning C, mainly with examples from Project 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. 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.
Would really appreciate if someone would have a look at this.
if (row == 8){ return 1; }don't you think this is not enough? The last row won't be ever solved. - Ajay BrahmakshatriyaisValid(sudokuNumber, row, col, nextNum)- avmohan