Skip to content
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

Create test.java #427

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
106 changes: 106 additions & 0 deletions test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
public class SudokuSolver {
public static boolean solveSudoku(int[][] board) {
int N = board.length;

if (!isUnsolved(board)) {
return true; // The Sudoku is already solved
}

for (int row = 0; row < N; row++) {
for (int col = 0; col < N; 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; // If this move leads to a solution, return true
}
board[row][col] = 0; // If not, reset and try the next number
}
}
return false; // If no number is valid, return false
}
}
}
return false; // To handle unsolvable puzzles
}

public static boolean isSafe(int[][] board, int row, int col, int num) {
return !usedInRow(board, row, num) &&
!usedInCol(board, col, num) &&
!usedInBox(board, row - row % 3, col - col % 3, num);
}

public static boolean usedInRow(int[][] board, int row, int num) {
for (int col = 0; col < board.length; col++) {
if (board[row][col] == num) {
return true;
}
}
return false;
}

public static boolean usedInCol(int[][] board, int col, int num) {
for (int row = 0; row < board.length; row++) {
if (board[row][col] == num) {
return true;
}
}
return false;
}

public static boolean usedInBox(int[][] board, int boxStartRow, int boxStartCol, int num) {
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
if (board[row + boxStartRow][col + boxStartCol] == num) {
return true;
}
}
}
return false;
}

public static boolean isUnsolved(int[][] board) {
int N = board.length;
for (int row = 0; row < N; row++) {
for (int col = 0; col < N; col++) {
if (board[row][col] == 0) {
return true;
}
}
}
return false;
}

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

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)) {
System.out.println("Sudoku Solved Successfully:");
printBoard(board);
} else {
System.out.println("No solution exists.");
}
}
}