美文网首页
valid Sudoku

valid Sudoku

作者: Kong_Mt | 来源:发表于2019-05-26 16:26 被阅读0次

英文原题

Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:

Each row must contain the digits 1-9 without repetition.
Each column must contain the digits 1-9 without repetition.
Each of the 9 3x3 sub-boxes of the grid must contain the digits 1-9 without repetition.


image.png

A partially filled sudoku which is valid.

The Sudoku board could be partially filled, where empty cells are filled with the character '.'.

Example 1:

Input:
[
["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"]
]
Output: true
Example 2:

Input:
[
["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"]
]
Output: false
Explanation: Same as Example 1, except with the 5 in the top left corner being
modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid.
Note:

A Sudoku board (partially filled) could be valid but is not necessarily solvable.
Only the filled cells need to be validated according to the mentioned rules.
The given board contain only digits 1-9 and the character '.'.
The given board size is always 9x9.

中文原题

判断一个9x9数独矩阵是否有效。数独的填充数只需要遵循以下条件即可:

  1. 数字1-9在每一行只能出现一次
  2. 数字1-9 在每一列只能出现一次
  3. 数字1-9在以粗线为边界的3x3宫内只能出现一次

解题代码

# 行循环
    x = 0
    y = 0
    row_dict = {}
    col_dict = {}
    block_dict = {}
    while x <= 8 and y <= 8:
        element = board[y][x]
        # 如果为非数字则跳过

        if element.isdigit():

            # 记录到当前行字典 -- 行监测
            if row_dict.get(x):
                container = row_dict[x]
                if element in container:
                    print('row error')
                    return False
                container.append(element)
            else:
                row_dict[x] = [board[y][x]]

            # 记录到当前列字典 -- 列监测
            if col_dict.get(y):
                container = col_dict[y]
                if element in container:
                    print('col error', y, ':', x)
                    return False
                container.append(element)
            else:
                col_dict[y] = [board[y][x]]

            # 记录到块字典 -- 区块监测
            block_pos = '{x}:{y}'.format(x=int(x // 3.0), y=int(y // 3.0))
            # print(block_pos)
            if block_dict.get(block_pos):
                container = block_dict[block_pos]
                if element in container:
                    return False
                container.append(element)
            else:
                block_dict[block_pos] = [board[y][x]]
        x += 1
        # 当x到达一行的行尾的时候指针重新指向行首,并且y指向下一行
        if x > 8:
            x = 0
            y += 1
        # print(x, ':', y)

    return True


if __name__ == '__main__':
    Test = [
        ["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"]
    ]

    Test2 = [
        ["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"]
    ]

    Test3 = [[".", ".", ".", ".", "5", ".", ".", "1", "."],
             [".", "4", ".", "3", ".", ".", ".", ".", "."],
             [".", ".", ".", ".", ".", "3", ".", ".", "1"],
             ["8", ".", ".", ".", ".", ".", ".", "2", "."],
             [".", ".", "2", ".", "7", ".", ".", ".", "."],
             [".", "1", "5", ".", ".", ".", ".", ".", "."],
             [".", ".", ".", ".", ".", "2", ".", ".", "."],
             [".", "2", ".", "9", ".", ".", ".", ".", "."],
             [".", ".", "4", ".", ".", ".", ".", ".", "."]
             ]
    print(isValidSudoku(Test3))

解题思路

先用一种o(n)的方式遍历矩阵,然后通过使用字典分别记录行,列,3x3矩阵,并且在循环中判重

相关文章

网友评论

      本文标题:valid Sudoku

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