diff --git a/test b/test new file mode 100644 index 00000000..9ddd70d6 --- /dev/null +++ b/test @@ -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."); + } + } +}