标签:数独 image bool src char vat lse img alt
class Solution {
boolean col[][];
boolean row[][];
boolean cell[][][];
public void solveSudoku(char[][] board) {
col = new boolean[9][9];
row = new boolean[9][9];
cell = new boolean[3][3][9];
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (board[i][j]!= '.') {
int t = board[i][j] - '0'-1; // 从 0开始数
col[j][t] = row[i][t] = cell[i / 3][j / 3][t] = true;
}
}
}
dfs(board , 0 ,0 );
}
private boolean dfs(char[][] board, int x, int y) {
if(y == 9 ) {x++; y = 0;}
if(x == 9) return true;
if(board[x][y] != '.') return dfs(board , x , y+1);
for(int i = 0 ; i < 9 ;i++) {
if(!col[y][i]&&!row[x][i]&&!cell[x/3][y/3][i]) {
board[x][y] = (char) ('1'+ i);
col[y][i] = row[x][i] = cell[x / 3][y / 3][i] = true;
if(dfs(board, x , y+1)) return true;
board[x][y] = '.';
col[y][i] = row[x][i] = cell[x / 3][y / 3][i] = false;
}
}
return false;
}
}
标签:数独 image bool src char vat lse img alt
原文地址:https://www.cnblogs.com/cznczai/p/11404434.html