Re: Récursivité
Sujet : Re: Récursivité
De : kurtzlepirate (at) *nospam* free.fr (kurtz le pirate)
Groupes : fr.comp.lang.cDate : 14. Jan 2025, 17:48:37
Autres entêtes
Organisation : compagnie de la banquise
Message-ID : <67869565$0$16845$426a74cc@news.free.fr>
References : 1 2 3 4 5
User-Agent : Mozilla Thunderbird
On 14/01/2025 01:18, beST wrote:
>
> Si elle te pose vraiment des problèmes, poste la, il y a bien un génie
> du C qui va te donner la bonne formule.
>
Oui, bien sûr, mais j'aurais bien voulu comprendre.
Mes différents essais aboutissent soit à une boucle infinie, soit à la "non" resolution.
Je ne sais plus ou j'ai trouvé cette fonction sur le Net.
C'est pour résoudre la résolution de Sudoku.
'a' passé en paramètre est un tableau 9x9, un grille de Sudoku
// ---------------------------------------------------------------------
// The recursive solve() procedure
// ---------------------------------------------------------------------
#macro Solve(a)
/*
// Counts the number of times the function calls itself.
#declare solve_count = solve_count+1;
#debug concat(" solve_count = ",str(solve_count,0,0),"\n")
*/
// Set default value do false : sudoku not solved by default
#local solve_value = false;
// Find an empty cell (the first one)
#local (Result,Row,Col) = find_empty(a);
// find_empty say that there is no empty cell
#if (!Result)
// all cells are filled.
// Sudoku is solved
// Set solve_value to true.
#local solve_value = true;
#else
// Yes, there is an empty cell at 'Row', 'Col'
// Let's try to fill it.
// For all digit from 1 to 9
#for (num,1,SIZE)
// If this candidate is good for this cell put it in this cell.
// is_valid() check that 'num' is not on the line 'Row', not in the
// column 'Col' an not in 3x3 square which contains (Row,Col).
#if ( is_valid(a,num,Row,Col) )
// Put 'num' in cell [Row][Col]
#declare a[Row][Col] = num;
// Tests if all cells are filled
// ! RECURSION HERE !
#if ( Solve(board) )
// Yes! Soduku is solved.
// Set the return value to true
#local solve_value = true;
// and exit this macros
#break
#end // #if ( solve(board) )
// Reset candidate 'num' in this cell
#declare a[Row][Col] = 0;
// the 'num' candidate is wrong
#end // #if ( is_valid(a,num,row,col) )
// next digit candidate
#end // #for (num,1,SIZE)
// look if there is an other empty cell
#end
// Return state of solver
solve_value
#end
// ---------------------------------------------------------------------
--
kurtz le pirate
compagnie de la banquise
Haut de la page
Les messages affichés proviennent d'usenet.
NewsPortal