leetcode 37 數獨
privateboolean[][]r=newboolean[9][9];
privateboolean[][]c=newboolean[9][9];
privateboolean[][][]x=newboolean[3][3][9];
privatebooleanvalid=false;
privateList<int[]>list=newArrayList<int[]>();
publicvoidsolveSudoku(char[][]board){
for(inti=0;i<9;i++){
for(intj=0;j<9;j++){
if(board[i][j]=='.'){
list.add(newint[]{i,j});
}
else{
intnum=Integer.parseInt(String.valueOf(board[i][j]))-1;
r[i][num]=c[j][num]=x[i/3][j/3][num]=true;
}
}
}
dfs(board,0);
}
privatevoiddfs(char[][]board,intpos){
if(pos==list.size()){
valid=true;
return;
}
int[]thisPos=list.get(pos);
intm=thisPos[0],n=thisPos[1];
for(inti=0;i<9&&!valid;i++){
if(!r[m][i]&&!c[n][i]&&!x[m/3][n/3][i]){
r[m][i]=c[n][i]=x[m/3][n/3][i]=true;
board[m][n]=(char)(i+'0'+1);
dfs(board,pos+1);
r[m][i]=c[n][i]=x[m/3][n/3][i]=false;
}
}
}