Voilà j'ai un projet à faire : résoudre des sudokus par la méthode du backtracking. J'ai codé les fonctions qui permettent de résoudre un sudoku mais ma fonction resolve ne fonctionne pas correctement. En effet, elle fonctionne très bien pendant un certain temps et malheureusement boucle sur deux case du tableau indéfiniment.
Je comprend pas du tout pourquoi peut être que vous pouvez m'aider.
Je vous donne le code source du solver... je pense que vous allez comprendre facilement.
Code:
1
2 typedef int* sudoku;
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 sudoku resolve(sudoku s, sudoku temp) { int index=0,value=0; while(index < 81) { index = next(s,index); value = value+1; printf("\n\n\n1 --> index : %d, value : %d\n",index,value); printSudoku(s); if(existInsert(s,value,index) == true) { s[index] = value; value = 0; } else if(value == 9) { value = 0; s[index] = 0; index = index-1; printf("\n\n\n2 --> index : %d, value : %d\n",index,value); printSudoku(s); while(isInSudokuBase(temp,index) == true) { index = index-1; } printf("\n\n\n3 --> index : %d, value : %d\n",index,value); printSudoku(s); if(s[index] < 9) { value = s[index]; s[index] = 0; printf("\n\n\n4 --> index : %d, value : %d\n",index,value); printSudoku(s); } } } return s; }
Si vous avez des doutes sur certaines fonctions n'hesitez pas à me demander ;)Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14 int main() { sudoku s = createSudoku(); sudoku temp = createSudoku(); initSudoku(s); initSudoku(temp); fileSudoku(s,temp,"sudoku.txt"); //test(s,temp); printSudoku(resolve(s,temp)); system("pause"); return 0; }