0% found this document useful (0 votes)
2 views2 pages

Sudoku Solver Java Code (2)

The document contains a Java implementation of a Sudoku solver using backtracking. It includes methods to solve the Sudoku puzzle, check the validity of numbers in the grid, and print the solved board. The main function initializes a Sudoku board and attempts to solve it, printing the result to the console.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Sudoku Solver Java Code (2)

The document contains a Java implementation of a Sudoku solver using backtracking. It includes methods to solve the Sudoku puzzle, check the validity of numbers in the grid, and print the solved board. The main function initializes a Sudoku board and attempts to solve it, printing the result to the console.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Sudoku Solver using Backtracking - Java Code

Java Code: Sudoku Solver

import java.util.*;

public class SudokuSolver {

public static boolean solve(char[][] board) {


for (int row = 0; row < 9; row++) {
for (int col = 0; col < 9; col++) {
if (board[row][col] == '.') {
for (char num = '1'; num <= '9'; num++) {
if (isValid(board, row, col, num)) {
board[row][col] = num;
if (solve(board)) return true;
board[row][col] = '.';
}
}
return false;
}
}
}
return true;
}

public static boolean isValid(char[][] board, int row, int col, char num) {
for (int i = 0; i < 9; i++) {
if (board[row][i] == num || board[i][col] == num) return false;
}

int startRow = 3 * (row / 3);


int startCol = 3 * (col / 3);

for (int i = startRow; i < startRow + 3; i++) {


for (int j = startCol; j < startCol + 3; j++) {
if (board[i][j] == num) return false;
}
}

return true;
}

public static void printBoard(char[][] board) {


for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
System.out.print(board[i][j] + " ");
}
System.out.println();
}
}

Page 1
Sudoku Solver using Backtracking - Java Code

public static void main(String[] args) {


char[][] board = {
{'5','3','.','.','7','.','.','.','.'},
{'6','.','.','1','9','5','.','.','.'},
{'.','9','8','.','.','.','.','6','.'},
{'8','.','.','.','6','.','.','.','3'},
{'4','.','.','8','.','3','.','.','1'},
{'7','.','.','.','2','.','.','.','6'},
{'.','6','.','.','.','.','2','8','.'},
{'.','.','.','4','1','9','.','.','5'},
{'.','.','.','.','8','.','.','7','9'}
};

if (solve(board)) {
System.out.println("Sudoku Solved:");
printBoard(board);
} else {
System.out.println("Sudoku Cannot Be Solved");
}
}
}

Page 2

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy