美文网首页
有效的数独

有效的数独

作者: 程序员生涯 | 来源:发表于2019-02-06 00:30 被阅读0次

    判断一个 9x9 的数独是否有效。只需要根据以下规则,验证已经填入的数字是否有效即可。

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

    上图是一个部分填充的有效的数独。

    数独部分空格内已填入了数字,空白格用 '.' 表示。

    示例 1:

    输入:
    [
    ["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"]
    ]
    输出: true

    示例 2:

    输入:
    [
    ["8","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"]
    ]
    输出: false
    解释: 除了第一行的第一个数字从 5 改为 8 以外,空格内其他数字均与 示例1 相同。
    但由于位于左上角的 3x3 宫内有两个 8 存在, 因此这个数独是无效的。

    说明:

    • 一个有效的数独(部分已被填充)不一定是可解的。
    • 只需要根据以上规则,验证已经填入的数字是否有效即可。
    • 给定数独序列只包含数字 1-9 和字符 '.'
    • 给定数独永远是 9x9 形式的。

    我自己给的算法:

        public static boolean isValidSudoku(char[][] board) {
            String row = "row";
            String col = "col";
            String block = "block";
            Map<String, Integer> sudokuCharsMaps = new HashMap<String, Integer>();
    
            for (int i = 0; i < 9; i++) {
                char[] arr2 = board[i];
    
                for (int j = 0; j < 9; j++) {
                    String temp = String.valueOf(arr2[j]);
    
                    if (!temp.equals(".")) {
                        if ((i >= 0 && i <= 2) && (j >= 0 && j <= 2)) {
                            if (null != sudokuCharsMaps.get(block + "33" + temp)) {
                                return false;
                            } else {
                                sudokuCharsMaps.put(block + "33" + temp, 1);
                            }
                        }
    
                        if ((i >= 0 && i <= 2) && (j >= 3 && j <= 5)) {
                            if (null != sudokuCharsMaps.get(block + "36" + temp)) {
                                return false;
                            } else {
                                sudokuCharsMaps.put(block + "36" + temp, 1);
                            }
                        }
    
                        if ((i >= 0 && i <= 2) && (j >= 6 && j <= 8)) {
                            if (null != sudokuCharsMaps.get(block + "39" + temp)) {
                                return false;
                            } else {
                                sudokuCharsMaps.put(block + "39" + temp, 1);
                            }
                        }
    
                        if ((i >= 3 && i <= 5) && (j >= 0 && j <= 2)) {
                            if (null != sudokuCharsMaps.get(block + "63" + temp)) {
                                return false;
                            } else {
                                sudokuCharsMaps.put(block + "63" + temp, 1);
                            }
                        }
    
                        if ((i >= 3 && i <= 5) && (j >= 3 && j <= 5)) {
                            if (null != sudokuCharsMaps.get(block + "66" + temp)) {
                                return false;
                            } else {
                                sudokuCharsMaps.put(block + "66" + temp, 1);
                            }
                        }
    
                        if ((i >= 3 && i <= 5) && (j >= 6 && j <= 8)) {
                            if (null != sudokuCharsMaps.get(block + "69" + temp)) {
                                return false;
                            } else {
                                sudokuCharsMaps.put(block + "69" + temp, 1);
                            }
                        }
    
                        if ((i >= 6 && i <= 8) && (j >= 0 && j <= 2)) {
                            if (null != sudokuCharsMaps.get(block + "93" + temp)) {
                                return false;
                            } else {
                                sudokuCharsMaps.put(block + "93" + temp, 1);
                            }
                        }
    
                        if ((i >= 6 && i <= 8) && (j >= 3 && j <= 5)) {
                            if (null != sudokuCharsMaps.get(block + "96" + temp)) {
                                return false;
                            } else {
                                sudokuCharsMaps.put(block + "96" + temp, 1);
                            }
                        }
    
                        if ((i >= 6 && i <= 8) && (j >= 6 && j <= 8)) {
                            if (null != sudokuCharsMaps.get(block + "99" + temp)) {
                                return false;
                            } else {
                                sudokuCharsMaps.put(block + "99" + temp, 1);
                            }
                        }
    
                        if (null != sudokuCharsMaps.get(row + i + temp) || null != sudokuCharsMaps.get(col + j + temp)) {
                            return false;
                        } else {
                            sudokuCharsMaps.put(row + i + temp, 1);
                            sudokuCharsMaps.put(col + j + temp, 1);
                        }
                    }
                }
            }
    
            return true;
        }
    

    不使用Map来实现的算法:

        public static boolean isValidSudoku2(char[][] board) {
            // 记录某行,某位数字是否已经被摆放
            boolean[][] row = new boolean[9][10];
            // 记录某列,某位数字是否已经被摆放
            boolean[][] col = new boolean[9][10];
            // 记录某 3x3 宫格内,某位数字是否已经被摆放
            boolean[][] block = new boolean[9][10];
            for (int i = 0; i < 9; i++) {
                for (int j = 0; j < 9; j++) {
                    if (board[i][j] != '.') {
                        int num = board[i][j] - '0';
                        if (row[i][num] || col[j][num] || block[i / 3 * 3 + j / 3][num]) {
                            return false;
                        } else {
                            row[i][num] = true;
                            col[j][num] = true;
                            block[i / 3 * 3 + j / 3][num] = true;
                        }
                    }
                }
            }
            return true;
        }
    

    上面这个算法有两个比较巧妙的地方,第一个是 int num = board[i][j] - '0'; 这行代码是字符相减(减 0),还是等于原字符;第二个地方是 block[i / 3 * 3 + j / 3][num] ,这里 i / 3 * 3 + j / 3 要注意,i / 3 * 3 的值并不等于 i !!
    我刚开始的算法收到这个启发,可以简化为:

        public static boolean isValidSudoku3(char[][] board) {
            String row = "row";
            String col = "col";
            String block = "block";
            Map<String, Integer> sudokuCharsMaps = new HashMap<String, Integer>();
    
            for (int i = 0; i < 9; i++) {
                char[] arr2 = board[i];
    
                for (int j = 0; j < 9; j++) {
                    String temp = String.valueOf(arr2[j]);
    
                    if (!temp.equals(".")) {
                        if (null != sudokuCharsMaps.get(row + i + temp) || null != sudokuCharsMaps.get(col + j + temp)
                                || null != sudokuCharsMaps.get(block + i / 3 * 3 + j / 3 + temp)) {
                            return false;
                        } else {
                            sudokuCharsMaps.put(row + i + temp, 1);
                            sudokuCharsMaps.put(col + j + temp, 1);
                            sudokuCharsMaps.put(block + i / 3 * 3 + j / 3 + temp, 1);
                        }
                    }
                }
            }
    
            return true;
        }
    

    相关文章

      网友评论

          本文标题:有效的数独

          本文链接:https://www.haomeiwen.com/subject/nrjbsqtx.html