LeetCode解题笔记 7 —— 37.解数独

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m940034240/article/details/88951442

问题

编写一个程序,通过已填充的空格来解决数独问题。

一个数独的解法需遵循如下规则

  1. 数字 1-9 在每一行只能出现一次。
  2. 数字 1-9 在每一列只能出现一次。
  3. 数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。

空白格用 '.' 表示。

一个数独。

答案被标成红色。

Note:

  • 给定的数独序列只包含数字 1-9 和字符 '.' 。
  • 你可以假设给定的数独只有唯一解。
  • 给定数独永远是 9x9 形式的。

解法

class Solution {
    public void solveSudoku(char[][] board) {
//        board = new char[][]{
//                {'5', '3', '.', '.', '7', '.', '.', '.', '.'},
//                {'6', '.', '.', '1', '9', '5', '.', '.', '.'},
//                {'.', '9', '8', '.', '.', '.', '.', '6', '.'},
//                {'8', '.', '.', '.', '6', '.', '.', '.', '3'},
//                {'4', '.', '.', '8', '.', '3', '.', '.', '1'},
//                {'7', '.', '.', '.', '2', '.', '.', '.', '6'},
//                {'.', '6', '.', '.', '.', '.', '2', '8', '.'},
//                {'.', '.', '.', '4', '1', '9', '.', '.', '5'},
//                {'.', '.', '.', '.', '8', '.', '.', '7', '9'}
//        };
        board = get(board);
        for (char[] chars : board){
            StringBuilder builder  = new StringBuilder();
            for (char c : chars){
                builder.append(c).append(",");
            }
            System.out.println(builder.toString());
        }
    }

    public char[][] get(char[][] board){
        List<Set<Character>> vertical = new ArrayList<>();//列Set
        List<Set<Character>> horizontal = new ArrayList<>();//行Set
        List<List<Set<Character>>> nine = new ArrayList<>();//九宫格Set
        for(int i = 0; i< board.length; i++){
            vertical.add(new HashSet<Character>());
            horizontal.add(new HashSet<Character>());
            if (i%3==0){
                List<Set<Character>> l = new ArrayList<>();
                for (int j = 0; j<3; j++){
                    l.add(new HashSet<Character>());
                }
                nine.add(l);
            }
        }
        for(int i = 0; i< board.length; i++){
            for (int j = 0; j<board.length;j++){
                if (board[i][j]!='.'){
                    vertical.get(i).add(board[i][j]);
                    horizontal.get(j).add(board[i][j]);
                    nine.get(i/3).get(j/3).add(board[i][j]);
                }
            }
        }
        put(vertical, horizontal, nine,board);
        return  board;
    }

    public boolean put(List<Set<Character>> vertical, List<Set<Character>> horizontal, List<List<Set<Character>>> nine,char[][] board){
        int x = 0 ,y = 0;
        boolean flag = false;
        //找出未填的位置
        for (int i = 0; i< board.length; i++) {
            for (int j = 0; j< board.length; j++){
                if (board[i][j]=='.'){
                   x = i;
                   y = j;
                   flag = true;
                }
            }
        }
        if (flag){
            char c = '0';
            for (int z = 0;z<9;z++){
                c++;
                if (!vertical.get(x).contains(c) && !horizontal.get(y).contains(c) && !nine.get(x/3).get(y/3).contains(c)){
                    vertical.get(x).add(c);
                    horizontal.get(y).add(c);
                    nine.get(x/3).get(y/3).add(c);
                    board[x][y] = c;
                    if (put(vertical, horizontal, nine,board)){
                        return true;
                    }else {
                        vertical.get(x).remove(c);
                        horizontal.get(y).remove(c);
                        nine.get(x/3).get(y/3).remove(c);
                        board[x][y] = '.';
                    }
                }
            }
            return false;
        }
        return true;
    }
}

猜你喜欢

转载自blog.csdn.net/m940034240/article/details/88951442