b********g 发帖数: 43 | 1 public void solveSudoku(char[][] board) {
// Start typing your Java solution below
// DO NOT write main() function
solveSudoku(board,0,0);
}
private boolean isConsistent(char[][] board, int col, int row, int n) {
for(int i=0;i<9;i++)
if(board[i][col]==(n+'0'))
return false;
for(int j=0;j<9;j++) {
if(board[row][j]==(n+'0'))
return false;
}
//check small box
int boxrow = (row/3)*3;
int boxcolumn = (col/3)*3;
for(int i=boxrow;i
for(int j=boxcolumn;j
if(board[i][j]==(n+'0'))
return false;
}
}
return true;
}
private void solveSudoku(char[][] board, int i, int j) {
if (i == 9) {
i = 0;
if (++j == 9)
return;
}
if (board[i][j] != '.') {// skip filled cells
solveSudoku(board,i+1,j);
return;
}
for (int val = 1; val <= 9; ++val) {
if (isConsistent(board,i,j,val)) {
board[i][j] = (char)(val+'0');
solveSudoku(board,i+1,j);
}
}
board[i][j] = '.'; // reset on backtrack
return;
} | l*********8 发帖数: 4642 | 2 if (i == 9) {
i = 0;
if (++j == 9)
return;
}
在return之前要把找到的解输出吧? | f*****i 发帖数: 835 | 3 看了半天,程序好像是没有什么问题,但是这个复杂度太高,可能算不出结果 |
|