1 题目:

根据给出的数独,全部填出来

 

 

2 思路:

 

为了做出来,我自己人工做了一遍题目给的数独。思路是看要填的数字横、竖、子是否已经有1-9的数字,有就剔除一个,最后剩下一个的话,就填上。一遍一遍的循环,直到填完为止。

后来发现,这个思路只能解决部分数独。还有部分数独是需要回溯的,比如,这个位置只能填3或5,那么就需要先填上3,看看能否继续填下去,不能的话,再回过来填5。

 

想了半天,想不出来,把别人的backtracking看懂了,写出来了。。

 

3 代码:

自己的:

    Hashtable<Integer, List<Character>> table;
    int row;
    int column;
    int totalNum;
    
    public void solveSudoku(char[][] board) {
        if (board.length == 0) {
            return;
        }
        
        row = board.length;
        column = board[0].length;
        totalNum = row * column;
        table = new Hashtable<Integer, List<Character>>(totalNum);
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                if (board[i][j] == '.') {
                    List<Character> validNum = new ArrayList<Character>();
                    Character[] nums = {'1','2','3','4','5','6','7','8','9'};
                    validNum.addAll(Arrays.asList(nums));
                    table.put(i*row+j, validNum);
                }else {
                    
                }
            }
        }
        
        fillBoard(board);
    }
    
    private void fillBoard(char[][] board)
    {
        for (int i = 0; i < row; i++) {
            System.out.println();
            for (int j = 0; j < column; j++) {
                int index = i*column + j;
                if (table.containsKey(index)) {
                    // 判断剩余的数字
                    calRemaingNum(i,j,index,board);
                }
                
            }
        }
        System.out.println("++++++++++++++++++++++++++++++++++++++");
        if (isSuccess()) {
            return;
        }
        fillBoard(board);
    }
    
    private void calRemaingNum(int i, int j, int calIndex,char[][] board)
    {
        //row
        for(int k=0; k< column; k++){
            if (board[i][k] != '.') {
                table.get(calIndex).remove((Character)board[i][k]);
            }
        }
        
        //column
        for (int k = 0; k < row; k++) {
            if (board[k][j] != '.') {
                table.get(calIndex).remove((Character)board[k][j]);
            }
        }
        
        //square
        int m = i / 3;
        int n = j / 3;
        for (int k = 0; k < 3; k++) {
            for (int k2 = 0; k2 < 3; k2++) {
                if (board[m*3+k][n*3+k2] != '.') {
                    table.get(calIndex).remove((Character)board[m*3+k][n*3+k2]);
                }
            }
        }
        System.out.println("i=" + i + " , j = " + j +  ",index = " + calIndex + "  ++ " + table.get(calIndex));
        if (table.get(calIndex).size() == 1) {
            board[i][j] = table.get(calIndex).get(0);
            table.remove(calIndex);
        }
    }
    
    private boolean isSuccess(){
        return table.size() == 0;
    }

 

 

别人的

 

     public void solveSudoku(char[][] board) {
         if (board==null || board.length == 0) {
             return;
        }
         
         fillboard(board);
     }
     
     private boolean fillboard(char[][] board){
         for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {
                if(board[i][j] == '.'){
                    for(char ch = '1'; ch <= '9'; ch++){
                        if (isValid(i, j, ch, board)) {
                            System.out.println("ok, i = " + i + ", j = " + j + " ,  value = " + ch);
                            board[i][j] = ch;
                            
                            if (fillboard(board)) {
                                return true;
                            }else {
                                board[i][j] = '.';
                            }
                        }
                    }
                    return false;
                }
            }
        }

         return true;
     }
    
     private boolean isValid(int row, int column, char value, char[][] board){
         // row
         for (int i = 0; i < board.length; i++) {
            if (board[row][i] == value) {
                return false;
            }
        }
         
         // column
         for (int i = 0; i < board[0].length; i++) {
            if (board[i][column] == value) {
                return false;
            }
        }
         
         // square
            int m = row / 3;
            int n = column / 3;
            for (int k = 0; k < 3; k++) {
                for (int k2 = 0; k2 < 3; k2++) {
                    if (board[m*3+k][n*3+k2] == value) {
                        return false;
                    }
                }
            }
         return true;
     }