我正在研究Valid Sudoku - LeetCode
并且无法弄清楚为什么 box_index = (i//3 ) * 3 + j//3
能够遍历子框
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 digits1-9
without repetition.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
.
阅读一个聪明的解决方案
class Solution:
def isValidSudoku(self, board):
"""
:type board: List[List[str]]
:rtype: bool
"""
# init data
rows = [{} for i in range(9)]
columns = [{} for i in range(9)]
boxes = [{} for i in range(9)]
# validate a board
for i in range(9):
for j in range(9):
num = board[i][j]
if num != '.':
num = int(num)
box_index = (i // 3 ) * 3 + j // 3
# keep the current cell value
rows[i][num] = rows[i].get(num, 0) + 1
columns[j][num] = columns[j].get(num, 0) + 1
boxes[box_index][num] = boxes[box_index].get(num, 0) + 1
# check if this value has been already seen before
if rows[i][num] > 1 or columns[j][num] > 1 or boxes[box_index][num] > 1:
return False
return True
测试用例
class MyCase(unittest.TestCase):
class MyCase(unittest.TestCase):
def setUp(self):
self.solution = Solution()
def test_a(self):
board = [ ["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"]
]
check = self.solution.isValidSudoku(board)
self.assertTrue(check)
def test_b(self):
board = [ ["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"]
]
check = self.solution.isValidSudoku(board)
self.assertFalse(check)
unittest.main() def setUp(self):
self.solution = Solution()
def test_a(self):
board = [ ["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"]
]
check = self.solution.isValidSudoku(board)
self.assertTrue(check)
def test_b(self):
board = [ ["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"]
]
check = self.solution.isValidSudoku(board)
self.assertFalse(check)
unittest.main()
能否请您提供一些提示,为什么 box_index = (i//3 ) * 3 + j//3
可以遍历子盒子?
最佳答案
您可以将整个盒子拆分为3*3
子盒子,(i, j)
属于索引为的子盒子>(i//3, j//3)
,这是一个 3*3
二维数组。如果我们想将其展平为 1*9
一维数组,索引将为 (i//3 ) * 3 + j//3
。
带有索引的子框:
|0|1|2|
|3|4|5|
|6|7|8|
如果你仍然感到困惑,你可以尝试一些例子,然后弄清楚。
希望对您有帮助,如有疑问请评论。 :)
关于python - 遍历有效数独的子框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55570466/