37. Sudoku Solver

来源:互联网 发布:淘宝手机模板 编辑:程序博客网 时间:2024/06/05 11:07

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.

求解数独。

根据规则,采用dfs求解即可。本题解法可以得出所有可能的解。

class Solution {        public void copyArray(char[][] des, char[][] source){        for (int i = 0; i < source.length; ++ i){            for (int j = 0; j < source[0].length; ++ j){                des[i][j] = source[i][j];            }        }    }        public boolean isValidFill(char[][] board, int row, int col, char c){        for (int i = 0; i < 9; ++ i){            if (board[row][i] != '.'&&board[row][i] == c){                return false;            }            if (board[i][col] != '.'&&board[i][col] == c){                return false;            }            if (board[3*(row/3) + i/3][3*(col/3) + i%3] != '.'&&board[3*(row/3) + i/3][3*(col/3) + i%3] == c){                return false;            }        }        return true;    }        public void dfs(char[][] tmp, char[][] board, int i){        if (i == 81){            copyArray(board, tmp);            return;        }        if (tmp[i/9][i%9] == '.'){            for (char c = '1'; c <= '9'; ++ c){                if (isValidFill(tmp, i/9, i%9, c)){                    tmp[i/9][i%9] = c;                    dfs(tmp, board, i + 1);                    tmp[i/9][i%9] = '.';                }            }        }        else {            dfs(tmp, board, i + 1);        }    }        public void solveSudoku(char[][] board) {        char[][] tmp = new char[9][9];        copyArray(tmp, board);        dfs(tmp, board, 0);    }}

由于题目已经假设只有一个解,所以也可以直接求解,只要在上面的结构上稍加限制即可。程序如下所示:

class Solution {    public boolean isValidFill(char[][] board, int row, int col, char c){        for (int i = 0; i < 9; ++ i){            if (board[row][i] != '.'&&board[row][i] == c){                return false;            }            if (board[i][col] != '.'&&board[i][col] == c){                return false;            }            if (board[3*(row/3) + i/3][3*(col/3) + i%3] != '.'&&board[3*(row/3) + i/3][3*(col/3) + i%3] == c){                return false;            }        }        return true;    }        public boolean dfs(char[][] board, int i){        if (i == 81){            return true;        }        if (board[i/9][i%9] == '.'){            for (char c = '1'; c <= '9'; ++ c){                if (isValidFill(board, i/9, i%9, c)){                    board[i/9][i%9] = c;                    if (dfs(board, i + 1)){                        return true;                    }                    else {                        board[i/9][i%9] = '.';                    }                }            }        }        else {            return dfs(board, i + 1);        }        return false;    }        public void solveSudoku(char[][] board) {        dfs(board, 0);    }}





原创粉丝点击