Write a program to solve a Sudoku puzzle by filling the empty cells.
A sudoku solution must satisfy all of the following rules:
- Each of the digits
1-9
must occur exactly once in each row. - Each of the digits
1-9
must occur exactly once in each column. - Each of the the digits
1-9
must occur exactly once in each of the 93x3
sub-boxes of the grid.
Empty cells are indicated by the character '.'
.
A sudoku puzzle...
...and its solution numbers marked in red.
Note:
- The given board contain only digits
1-9
and the character'.'
. - You may assume that the given Sudoku puzzle will have a single unique solution.
- The given board size is always
9x9
.
public static void main(String[] args) {
char[][] board = new char[][] { { '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' } };
// char[][] board = new char[][]{{'.','.','9','7','4','8','.','.','.'},{'7','.','.','.','.','.','.','.','.'},{'.','2','.','1','.','9','.','.','.'},{'.','.','7','.','.','.','2','4','.'},{'.','6','4','.','1','.','5','9','.'},{'.','9','8','.','.','.','3','.','.'},{'.','.','.','8','.','3','.','2','.'},{'.','.','.','.','.','.','.','.','6'},{'.','.','.','2','7','5','9','.','.'}};
print(board);
solveSudoku(board);
print(board);
}
private static void print(char[][] board) {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board.length; j++) {
System.out.print(board[i][j] + " ");
}
System.out.println();
}
System.out.println("==========================================");
}
public static boolean isValidSudoku(char[][] board) {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board.length; j++) {
if (board[i][j] != '.') {
for (int i0 = i + 1; i0 < board.length; i0++) {
if (board[i0][j] == board[i][j])
return false;
}
for (int j0 = j + 1; j0 < board.length; j0++) {
if (board[i][j0] == board[i][j])
return false;
}
int gi = i / 3;
for (int i0 = gi * 3; i0 / 3 == gi; i0++) {
if (i0 == i)
continue;
int gj = j / 3;
for (int j0 = gj * 3; j0 / 3 == gj; j0++) {
if (j0 == j)
continue;
if (board[i0][j0] == board[i][j])
return false;
}
}
}
}
}
return true;
}
public static boolean isValidSudoku(char[][] board, int i, int j) {
for (int i0 = 0; i0 < board.length; i0++) {
if(i0 == i)
continue;
if (board[i0][j] == board[i][j])
return false;
}
for (int j0 = 0; j0 < board.length; j0++) {
if(j0 == j)
continue;
if (board[i][j0] == board[i][j])
return false;
}
int gi = i / 3;
for (int i0 = gi * 3; i0 / 3 == gi; i0++) {
if (i0 == i)
continue;
int gj = j / 3;
for (int j0 = gj * 3; j0 / 3 == gj; j0++) {
if (j0 == j)
continue;
if (board[i0][j0] == board[i][j])
return false;
}
}
return true;
}
public static char[][] solveSudoku(char[][] board) {
if(!isValidSudoku(board)) {
return null;
}
if(fillSudoku(board, 0, 0))
return board;
else
return null;
}
private static boolean fillSudoku(char[][] board, int i, int j) {
if(i == board.length && j == 0) {
return true;
}
if (board[i][j] == '.') {
char tryNum = '1';
while (tryNum <= '9') {
board[i][j] = tryNum;
if(isValidSudoku(board, i, j)){
if(j == board.length - 1) {
if(fillSudoku(board, i + 1, 0)) {
return true;
}
} else {
if(fillSudoku(board, i, j + 1)) {
return true;
}
}
}
tryNum ++;
}
board[i][j] = '.';
} else {
if(j == board.length - 1) {
return fillSudoku(board, i + 1, 0);
} else {
return fillSudoku(board, i, j + 1);
}
}
return false;
}