Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3304386
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:03:24+00:00 2026-05-17T21:03:24+00:00

Everything seems to work fine in the algorithm besides the solve method. When it

  • 0

Everything seems to work fine in the algorithm besides the solve method. When it executes the program using a solvable Sudoku board, it says that it cannot be solved. I’ve tried everything I can think of in the solve method. I’ve tried debugging and it fails after the first row is tested. Any suggestions? Here is the full code so far:

    public class SudokuSolver {

 public static void initializeGrid(int[][] grid, int[][] puzzle) {

  for (int r = 0; r < puzzle.length; r++) {
  for (int c = 0; c < puzzle[0].length; c++) {
  grid [r][c] = puzzle [r][c];
   }
  } 
 }

 public static void displayGrid(int[][] grid) {

  for (int r = 0; r < grid.length; r++) {
   if (r % 3 == 0) {
   System.out.println("+---+---+---+");
   }
   for (int c = 0; c < grid[r].length; c++) {
if (c % 3 == 0) {
 System.out.print("|");
}
displayDigits(r, c, grid);

}
System.out.print("|");
System.out.println();
}
System.out.println("+---+---+---+");
}

if (grid[r][c] == 0) {
System.out.print(' ');
}
else {
System.out.print(grid[r][c]);
}
}
public static int getEmptyCells(int[][] grid, int[][] emptyCells) {
int i = 0;
int numEmptyCells = 0;
for (int r = 0; r < grid.length; r++) {
for (int c = 0; c < grid[r].length; c++) {
if (grid[r][c] == 0) {
emptyCells[i][0] = r;
emptyCells[i][1] = c;
numEmptyCells++;
i++;
}
}
}
return numEmptyCells;
}

private static boolean hasNoDuplicates(int[] digitsList) {
for (int j = 0; j < digitsList.length; j++) {
for (int k = j + 1; k < digitsList.length; k++) {
if (digitsList[j] == digitsList[k] && digitsList[j] != 0)
return false;
}
}
return true;
}

private static boolean checkCurrentRow(int[][] grid, int currentRow) {
int[] digitsList = new int[grid.length];
for (int c = 0; c < digitsList.length; c++) {
digitsList[c] = grid[currentRow][c];
}
if (hasNoDuplicates(digitsList)) {
return true;
}
return false;
}

private static boolean checkCurrentCol(int[][] grid, int currentCol) {
int[] digitsList = new int[grid.length];
for (int i = 0; i < digitsList.length; i++) {
digitsList[i] = grid[i][currentCol];
}
if (hasNoDuplicates(digitsList)) {
return true;
}
return false;
}

private static boolean checkCurrentRegion(int[][] grid, int currentRow, int currentCol) {
int[] digitsList = new int[grid.length];
currentRow = (currentRow / 3) * 3;
currentCol = (currentCol / 3) * 3;
int i = 0;
for (int r = 0; r < 3; r++) {
for (int c = 0; c < 3; c++) {
digitsList[i] = grid[currentRow + r][currentCol + c];
i++;
}
}
if (hasNoDuplicates(digitsList)) {
return true;
}
return false;
}

public static boolean isConsistent(int[][] grid, int currentRow, int currentCol) {
if (checkCurrentRow(grid, currentRow) && checkCurrentCol(grid, currentCol)
&& checkCurrentRegion(grid, currentRow, currentCol)) {
return true;
}
return false;
}

public static boolean solvePuzzle(int[][] grid, int[][] emptyCells, int numEmptyCells) {
int i = 0;
int j = 0;
int currentCellDigit = grid[emptyCells[i][0]][emptyCells[i][1]];
while (j < numEmptyCells) {
if (currentCellDigit != 9) {
currentCellDigit++;
grid[emptyCells[i][0]][emptyCells[i][1]] = currentCellDigit;
if (isConsistent(grid, emptyCells[i][0], emptyCells[i][1])) {
grid[emptyCells[i][0]][emptyCells[i][1]] = currentCellDigit;
i++;
j++;
}
else {
grid[emptyCells[i][0]][emptyCells[i][1]] = currentCellDigit - 1;
}
}
else {
currentCellDigit = 0;
currentCellDigit = grid[emptyCells[i][0]][emptyCells[i][1]];
i--;
j--;
if (j < 0) {
return false;
}
}
}

return true;
}

public static void main(String[] args) {

final int SIZE = 9;
int[][] puzzle = { {0,2,9,0,0,3,0,0,5},
{5,0,7,0,0,0,0,9,0},
{6,0,0,0,0,9,4,2,0},
{3,0,2,0,0,4,0,0,0},
{0,0,5,0,3,0,7,0,0},
{0,0,0,5,0,0,6,0,2},
{0,9,8,4,0,0,0,0,3},
{0,3,0,0,0,0,1,0,6},
{2,0,0,3,0,0,9,4,0}
};

int[][] grid = new int[SIZE][SIZE];
int[][] emptyCellsList = new int[SIZE*SIZE][2];
int numEmptyCells = 0;

initializeGrid(grid, puzzle);
numEmptyCells = getEmptyCells(grid, emptyCellsList);
System.out.println("The puzzle:");
displayGrid(puzzle);
if (solvePuzzle(grid, emptyCellsList, numEmptyCells)) {
System.out.println("has been solved:");
displayGrid(grid);
}
else {
System.out.println("cannot be solved!");
}
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T21:03:24+00:00Added an answer on May 17, 2026 at 9:03 pm

    Your initializeGrid is wrong. IMHO, it should be:

    for (int c = 0; c < puzzle[r].length; c++)
    

    instead of

    for (int c = 0; c < puzzle[0].length; c++)
    

    EDIT: my answer below this

    It’s been indented properly (lesson 1) and arranged the curly braces accordingly (lesson 2). Learn to understand what you are trying to do line by line and when you are stuck on a specific line or method call, look for help (lesson 3). The code below will compile but will not (yet) solve the puzzle. Read and replace the comments inside solvePuzzle for me (lesson 4). Do some thinking and analyzing since this is your homework 😉 Good luck!

    public class SudokuSolver {
      public static void initializeGrid(int[][] grid, int[][] puzzle) {
       for (int r = 0; r < puzzle.length; r++) {
         for (int c = 0; c < puzzle[0].length; c++) {
           grid [r][c] = puzzle [r][c];
         }
       } 
      }
      
      public static void displayDigits(int r, int c, int[][] grid) {
        if (grid[r][c] == 0) {
          System.out.print('0');
        } else {
          System.out.print(grid[r][c]);
        }
      }
    
      public static void displayGrid(int[][] grid) {
        for (int r = 0; r < grid.length; r++) {
          if (r % 3 == 0) {
            System.out.println("+---+---+---+");
          }
          for (int c = 0; c < grid[r].length; c++) {
            if (c % 3 == 0) {
              System.out.print("|");
            }
          displayDigits(r, c, grid);
          }
          System.out.print("|");
          System.out.println();
        }
        System.out.println("+---+---+---+");
      }
     
      public static int getEmptyCells(int[][] grid, int[][] emptyCells) {
        int i = 0;
        int numEmptyCells = 0;
        for (int r = 0; r < grid.length; r++) {
           for (int c = 0; c < grid[r].length; c++) {
             if (grid[r][c] == 0) {
               emptyCells[i][0] = r;
               emptyCells[i][1] = c;
               numEmptyCells++;
               i++;
             }
           }
        }
        return numEmptyCells;
      }
      
      private static boolean hasNoDuplicates(int[] digitsList) {
        for (int j = 0; j < digitsList.length; j++) {
          for (int k = j + 1; k < digitsList.length; k++) {
            if (digitsList[j] == digitsList[k] && digitsList[j] != 0)
              return false;
          }
        }
        return true;
      }
    
      private static boolean checkCurrentRow(int[][] grid, int currentRow) {
        int[] digitsList = new int[grid.length];
        for (int c = 0; c < digitsList.length; c++) {
          digitsList[c] = grid[currentRow][c];
        }
        if (hasNoDuplicates(digitsList)) {
          return true;
        }
        return false;
      }
    
      private static boolean checkCurrentCol(int[][] grid, int currentCol) {
        int[] digitsList = new int[grid.length];
        for (int i = 0; i < digitsList.length;  i++) {
          digitsList[i] = grid[i][currentCol];
        }
        if (hasNoDuplicates(digitsList)) {
          return true;
        }
        return false;
      }
    
      private static boolean checkCurrentRegion(int[][] grid, int currentRow,
           int currentCol) {
        int[] digitsList = new int[grid.length];
        currentRow = (currentRow / 3) * 3;
        currentCol = (currentCol / 3) * 3;
        int i = 0;
        for (int r = 0; r < 3; r++) {
          for (int c = 0; c < 3; c++) {
            digitsList[i] = grid[currentRow + r][currentCol + c];
            i++;
          }
        }
        if (hasNoDuplicates(digitsList)) {
          return true;
        }
        return false;
      }
    
      public static boolean isConsistent(int[][] grid, int currentRow,
           int currentCol) {
        boolean checkRow = checkCurrentRow(grid, currentRow);
        boolean checkCol = checkCurrentCol(grid, currentCol);
        boolean checkReg = checkCurrentRegion(grid, currentRow, currentCol);
        System.out.println("r: " + checkRow + " c: " + checkCol + " r: " + checkReg);
        if (checkRow && checkCol && checkReg) {
          return true;
        }
        return false;
      }
    
      public static boolean solvePuzzle(int[][] grid, int[][] emptyCells,
          int numEmptyCells) {
        int i = 0;
        int currentCellDigit = 0;
        while (i < numEmptyCells) {
          if (currentCellDigit != 9) {
            // increment cell value
            currentCellDigit++;
            // assign to current cell the current cell value
            //<var> = currentCellDigit;
            System.out.println("Solving---------------------- :" +
                currentCellDigit + " R: " +
                emptyCells[i][0] + " C: " + emptyCells[i][1]);
            // check if value is valid
            if (isConsistent(grid, emptyCells[i][0], emptyCells[i][1])) {
              // reset after setting
              i++;
              currentCellDigit = 0;
            } else {
              // reset cell to zero instead of decrementing it since we're backtracking!
              //grid[emptyCells[i][0]][emptyCells[i][1]] = currentCellDigit - 1;
              //<var> = 0;
            }
          } else {
            // reset current cell to 0
            //<var> = 0;
            // go to previous cell
            i--;
            // exit if theres no cell to backtrack to
            if (i < 0) {
              return false;
            }
            // set previous' cell value as current cell value
            //<var> = grid[emptyCells[i][0]][emptyCells[i][1]];
          }
        }
        return true;
      }
      
      public static void main(String[] args) {
        final int SIZE = 9;
        int[][] puzzle  = { {0,2,9,0,0,3,0,0,5},
                            {5,0,7,0,0,0,0,9,0},
                            {6,0,0,0,0,9,4,2,0},
                            {3,0,2,0,0,4,0,0,0},
                            {0,0,5,0,3,0,7,0,0},
                            {0,0,0,5,0,0,6,0,2},
                            {0,9,8,4,0,0,0,0,3},
                            {0,3,0,0,0,0,1,0,6},
                            {2,0,0,3,0,0,9,4,0}
                        };
    
        int[][] grid = new int[SIZE][SIZE];
        int[][] emptyCellsList = new int[SIZE*SIZE][2];
        int numEmptyCells = 0;
    
        initializeGrid(grid, puzzle);
        numEmptyCells = getEmptyCells(grid, emptyCellsList);
        System.out.println("The puzzle:");  
        displayGrid(puzzle);
        if (solvePuzzle(grid, emptyCellsList, numEmptyCells)) {
          System.out.println("has been solved:");
          displayGrid(grid);
        } else {
          System.out.println("cannot be solved!");
        }
      }
    }
    

    P.S. this will do backtracking if you’ve replaced the comments above with proper code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to re-install a DLL in the GAC, everything seems to work fine
I implemented OpenID support for an ASP.Net 2.0 web application and everything seems to
I have tried what seems like everything - I've done similiar things many times
In Firefox a Reload seems to reload everything while on IE the Refresh reloads
Everything I have read says that when making a managed stored procedure, to right
I seem to recall that there is an HTML tag that escapes absolutely everything
The people on this website seem to know everything so I figured I would
Everything inherits from object. It's the basis of inheritance. Everything can be implicitly cast
Everything I read about better PHP coding practices keeps saying don't use require_once because
Everything I can find in linq for aggregation has a group by clause. How

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.