class Solution {
public void solveSudoku(char[][] board) {
if (board == null || board.length == 0) return;
solve(board);
}
private boolean solve(char[][] board){
int n = board.length, m = board[0].length;
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
if (board[i][j] == '.'){
for (char c = '1'; c <= '9'; c++){
if (valid(board, i, j, c)){
board[i][j] = c;
if (solve(board)){
return true;
}
else{
// otherwise go back
board[i][j] = '.';
}
}
}
//if tried 1-9, but still cannot fill this blank, then return false
return false;
}
}
}
return true;
}
private boolean valid(char[][] board, int row, int col, char c){
//row
for (int i = 0; i < 9; i++){
if (board[row][i] == c) return false;
}
//col
for (int i = 0; i < 9; i++){
if (board[i][col] == c) return false;
}
//cell
for (int i = 0; i < 9; i++){
if (board[3 * (row / 3) + i / 3][3 * (col / 3) + i % 3] == c) return false;
}
return true;
}
}