Skip to content

Added a new Feature called Sudoku Solver by using Backtracking and Re… #5698

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions src/main/java/com/thealgorithms/backtracking/SudokuSolver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.thealgorithms.backtracking;

/**
* Solves a Sudoku of any level and prints solved Sudoku
* @author Indraneela Doradla (<a href="https://github.com/captiosus1">git-Indraneela Doradla</a>)
*/
public final class SudokuSolver {

private SudokuSolver() {
// Private constructor to prevent instantiation
}

/**
* Solves the Sudoku using backtracking
* @param board the Sudoku grid
* @return boolean indicating if the Sudoku can be solved
*/
public static boolean solveSudoku(int[][] board) {
int r = -1, c = -1;
boolean isEmpty = true;

// Finding the first empty position
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (board[i][j] == 0) {
r = i;
c = j;
isEmpty = false;
break;
}
}
if (!isEmpty) break;
}

// If no empty position is found, the Sudoku is solved
if (isEmpty) {
return true;
}

// Try filling the empty position with numbers from 1 to 9
for (int num = 1; num <= 9; num++) {
if (isSafe(board, r, c, num)) {
board[r][c] = num;
if (solveSudoku(board)) {
return true; // Successfully solved
}
board[r][c] = 0; // Backtrack
}
}

return false; // No solution found, backtrack
}

/**
* Checks if placing a number at the given position is valid
* @param board the Sudoku grid
* @param r row index
* @param c column index
* @param val the value to place
* @return boolean indicating if it's safe to place the value
*/
public static boolean isSafe(int[][] board, int r, int c, int val) {
// Check the row and column
for (int i = 0; i < board.length; i++) {
if (board[i][c] == val || board[r][i] == val) {
return false;
}
}

// Check the 3x3 subgrid
int sqrt = (int) Math.sqrt(board.length);
int boxRowStart = r - r % sqrt;
int boxColStart = c - c % sqrt;

for (int i = boxRowStart; i < boxRowStart + sqrt; i++) {
for (int j = boxColStart; j < boxColStart + sqrt; j++) {
if (board[i][j] == val) {
return false;
}
}
}

return true;
}

/**
* Prints the Sudoku grid
* @param board the Sudoku grid
*/
public static void printSudoku(int[][] board) {
for (int[] row : board) {
for (int num : row) {
System.out.print(num + " ");
}
System.out.println();
}
System.out.println();
}
}
Loading