36. Sudoku Solver

Write a program to solve a Sudoku puzzle by filling the empty cells.

Empty cells are indicated by the character '.'.

You may assume that there will be only one unique solution.

A sudoku puzzle...

 

...and its solution numbers marked in red.

---

 

    public boolean solveSudoku(char[][] board) {
        
        int n = board.length;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (board[i][j] == '.') {
                    for (int k = 0; k < n; k++) {
                        board[i][j] = (char) ('1' + k);
                        if (isValid(board, i, j, n) && solveSudoku(board))
                            return true;
                        board[i][j] = '.';
                    }
                    return false;
                }
            }
        }
        return true;
    }

用35 valid sudoku检查

posted @ 2013-09-22 11:52  LEDYC  阅读(151)  评论(0)    收藏  举报