简体   繁体   中英

4x4 Sudoku Solver in C code doesnt produce any output

I am trying my hands at making a 4x4 Sudoku solver but I do not understand why the code produce any output. Is the loop running for too long or have I done a stupid yet fatal mistake in the do while loop. I do not seem to get into running the anotherround() function.

Here is my first attempt. The logic should be fine.

#include <stdio.h>
#define SIZE 4

void printBoard(int[][SIZE]);
void solve(int[][SIZE]);
int anotherRound(int[][SIZE]);
void checkBox(int[][SIZE], int, int, int, int);

int main(void)
{
  int board[4][4] = {
    {0,3,4,0},
    {4,0,0,2},
    {1,0,0,3},
    {0,2,1,0}
  };

  solve(board); // solve the puzzle now!

  printf("Sudoku puzzle solved:\n");
  printBoard(board);

  return 0;
}

// To print the 4x4 Sudoku board.
void printBoard(int board[][SIZE])
{
  int r, c;

  for (r = 0; r < SIZE; r++)
  {
    for (c = 0; c < SIZE; c++)
      printf("%d ", board[r][c]);
    printf("\n");
  }
}

// Solving a Sudoku puzzle using a very simple
// algorithm (algorithm A in the write-up).
// It only solves the simplest puzzles.
void solve(int board[][SIZE])
{
  int blankCellFound; // indicate whether there is still some blank cell

  do
  {
    blankCellFound = anotherRound(board);
  } while (blankCellFound == 1);
}

// Apply loop body of algorithm A and return 1 if there are still
// blank cells, or 0 if there is no more blank cell.
int anotherRound(int board[][SIZE])
{
  int r, c,
      sum,       // Sum of elements in a row or column
      countZero; // Number of zeroes in a row or column

  // Check every row
  for (r = 0; r < SIZE; r++)
  {
    sum = 0;
    countZero = 0;

    for (c = 0; c < SIZE; c++)
    {
      if (board[r][c] == 0)
        countZero++;
      else
        sum += board[r][c];
    }
    if (countZero == 1)
    { // A single zero is present
      for (c = 0; c < SIZE; c++)
      { // To find the position of the single zero
        if (board[r][c] == 0)
        {
          board[r][c] = 10 - sum; // Replace the single zero with the obvious value
          break;
        }
      }
    }
  }
  // Check every column
  for (c = 0; c < SIZE; c++)
  {
    sum = 0;
    countZero = 0;

    for (r = 0; r < SIZE; r++)
    {
      if (board[r][c] == 0)
        countZero++;
      else
        sum += board[r][c];
    }
    if (countZero == 1)
    { // A single zero is present
      for (r = 0; r < SIZE; r++)
      { // To find the position of the single zero
        if (board[r][c] == 0)
        {
          board[r][c] = 10 - sum; // Replace the single zero with the obvious value
          break;
        }
      }
    }
  }
  // Check every box
  checkBox(board, 0, 2, 0, 2);
  checkBox(board, 0, 2, 2, 4);
  checkBox(board, 2, 4, 0, 2);
  checkBox(board, 2, 4, 2, 4);

  // Check if there are any blank cells
  for (r = 0; r < SIZE; r++)
  {
    for (c = 0; c < SIZE; c++)
    {
      if (board[r][c] == 0) // Blank cell found
        return 1;
    }
  }
  return 0; // No blank cell
}
// This function checks a 2X2 box if there is a single zero.
// If so, it replaces the zero with the obvious value.
// Precond: lrow <= urow, lcol <= ucol, 0 <= lrow, urow, lcol, ucol <= SIZE
void checkBox(int board[][SIZE], int lrow, int urow, int lcol, int ucol)
{
  int r, c,
      sum = 0,       // Sum of elements in the 2X2 box
      countZero = 0; // Number of zeroes in the 2X2 box

  for (r = lrow; r < urow; r++)
  { // lrow and urow are the lower and upper limits of the rows
    for (c = lcol; c < ucol; c++)
    { // lcol and ucol are the lower and upper limits of the columns
      if (board[r][c] == 0)
        countZero++;
      else
        sum += board[r][c];
    }
  }
  if (countZero == 1)
  { // A single zero is present
    for (r = lrow; r < urow; r++)
    {
      for (c = lcol; c < ucol; c++)
      { // To find the position of the single zero
        if (board[r][c] == 0)
        {
          board[r][c] = 10 - sum; // Replace the single zero with the obvious value
          break;
        }
      }
    }
  }
} ``` 

As you wrote, this algorithm can only solve the simplest puzzles.

Basically, it checks if there is a single zero in any row, column or 2x2 box, and then replace this zero with the missing value.

However, you test it with a more complex puzzle, when no row, column or box have this property. It therefore enter in an infinite loop, in the solve(.) function.

For example, your code provides the correct answer, with this slightly modified puzzle:

  int board[4][4] = {
    {2,3,4,0},
    {4,0,0,2},
    {1,0,0,3},
    {0,2,1,0}
  };

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