Sudoku Solver in Java

 

 

This document provides a simple implementation of a Sudoku solver using the backtracking algorithm in Java. The program checks for valid placements of numbers in a Sudoku grid and solves the puzzle recursively.

Program Structure

  • Class: SudokuSolver – This is the main class containing methods for solving the Sudoku.
  • Method: solveSudoku – The main method that initiates the solving process.
  • Method: isSafe – Checks if placing a number in a specific cell is valid according to Sudoku rules.
  • Method: printBoard – Prints the Sudoku board to the console.
  • Method: main – Entry point of the program that initializes the Sudoku board.

Java Code


public class SudokuSolver {
    
    public static void main(String[] args) {
        int[][] board = {
            {5, 3, 0, 0, 7, 0, 0, 0, 0},
            {6, 0, 0, 1, 9, 5, 0, 0, 0},
            {0, 9, 8, 0, 0, 0, 0, 6, 0},
            {8, 0, 0, 0, 6, 0, 0, 0, 3},
            {4, 0, 0, 8, 0, 3, 0, 0, 1},
            {7, 0, 0, 0, 2, 0, 0, 0, 6},
            {0, 6, 0, 0, 0, 0, 2, 8, 0},
            {0, 0, 0, 4, 1, 9, 0, 0, 5},
            {0, 0, 0, 0, 8, 0, 0, 7, 9}
        };
        
        if (solveSudoku(board)) {
            printBoard(board);
        } else {
            System.out.println("No solution exists.");
        }
    }

    public static boolean solveSudoku(int[][] board) {
        for (int row = 0; row < 9; row++) {
            for (int col = 0; col < 9; col++) {
                if (board[row][col] == 0) {
                    for (int num = 1; num <= 9; num++) {
                        if (isSafe(board, row, col, num)) {
                            board[row][col] = num;
                            if (solveSudoku(board)) {
                                return true;
                            }
                            board[row][col] = 0; // backtrack
                        }
                    }
                    return false; // trigger backtrack
                }
            }
        }
        return true; // solved
    }

    public static boolean isSafe(int[][] board, int row, int col, int num) {
        // Check row
        for (int x = 0; x < 9; x++) {
            if (board[row][x] == num) return false;
        }
        // Check column
        for (int x = 0; x < 9; x++) {
            if (board[x][col] == num) return false;
        }
        // Check 3x3 box
        int startRow = row - row % 3, startCol = col - col % 3;
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                if (board[i + startRow][j + startCol] == num) return false;
            }
        }
        return true;
    }

    public static void printBoard(int[][] board) {
        for (int r = 0; r < 9; r++) {
            for (int d = 0; d < 9; d++) {
                System.out.print(board[r][d]);
                System.out.print(" ");
            }
            System.out.print("\n");
        }
    }
}

Documentation

1. main

The entry point of the program, it initializes a Sudoku board and calls the solveSudoku method.

2. solveSudoku(int[][] board)

This method implements the backtracking algorithm to solve the Sudoku puzzle. It iterates through each cell, checking for empty cells (represented by 0), and attempts to place numbers from 1 to 9. If a number placement leads to a solution, it returns true; otherwise, it backtracks.

3. isSafe(int[][] board, int row, int col, int num)

This method checks if a number can be placed at the specified row and column without violating Sudoku rules (same number not appearing in the same row, column, or 3×3 box).

4. printBoard(int[][] board)

This method prints the current state of the Sudoku board to the console.

Conclusion

This simple Sudoku solver demonstrates the backtracking technique and basic array manipulation in Java. With further enhancements, you can add features like user input for the board or graphical representation.

 

Leave a Reply

Your email address will not be published. Required fields are marked *