java

数独

http://www.nowcoder.com/questionTerminal/5e6c424b82224b85b64f28fd85761280

public class Solution {
    public void solveSudoku(char[][] board) {
        dfs(board,0,0);
    }
    boolean dfs(char[][] board,int x,int y){
        if(x==9)return true;
        if(y==9)return dfs(board,x+1,0);
        if(board[x][y] != '.')return dfs(board,x,y+1);
        for(char c='1';c<='9';++c){
            if(!isValid(board,x,y,c))continue;
            board[x][y]=c;
            if(dfs(board,x,y+1))return true;
            board[x][y] = '.';
        }
        return false;
    }
    boolean isValid(char[][] board,int x,int y,char ch){
        for(int i=0;i<9;++i){
            if(board[x][i] == ch)return false;
            if(board[i][y] == ch)return false;
            if(board[(x/3)*3+ i/3][(y/3)*3 + i%3]==ch)return false;
        }
        return true;
    }
}
全部评论

相关推荐

我已成为0offer的糕手:别惯着,胆子都是练出来的,这里认怂了,那以后被裁应届被拖工资还敢抗争?
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务