Sudoku Solver Solution for Backtracking Failure

Suppose a two-dimensional array containing a 9x9 sudoku grid, where is my decisive function destroyed? I am trying to solve this using a simple backtracking approach. Thanks!

bool solve(int grid[9][9])
{
 int i,j,k;
 bool isSolved = false;

  if(!isSolved(grid))
   isSolved = false;

  if(isSolved)
   return isSolved;

  for(i=0; i<9; i++)
  {
   for(j=0; j<9; j++)
   {
    if(grid[i][j] == 0)
    {
     for(k=1; k<=9; k++)
     {
      if(legalMove(grid,i,j,k))
      {
       grid[i][j] = k;
       isSolved = solve(grid);
       if (isSolved)
        return true;
      }
      grid[i][j] = 0;
     }
     isSolved = false;
    }
   }
  }

return isSolved;
}

Even after changing the problems, isSolvedmy solution seems to break into an infinite loop. It seems like I'm skipping some basic step, but I'm not sure where and why. I examined similar solutions and still can not determine the problem. I'm just trying to create a basic solver, no need to go for efficiency. Thanks for the help!

+3
source share
3 answers

, . .

bool isSolved = false;

if(!isSolved(grid))
    isSolved = false;

if(isSolved)
    return isSolved;

, isSolved ​​ true,

if(isSolved)
    return isSolved;

.

, , . , 9 * 9 * 9 = 729 , . n 729 ^ n . , , , , , , , 90% , , , ? , k , k - (k <= 10), ( k ^ n.)

, "" , , , . , , , - , (, A *.)

sudoku, , 100x100 .

- " " 9x9, , .

, , , , , , , . , , .

+4

isSolved , . , , .

.

+3

, , , "0" " [i] [j] = 0;" . , "else" THEN "grid [i] [j] = 0;"

+2

Source: https://habr.com/ru/post/1719351/


All Articles