## 如何检查Sudoku板上的所有子网格，而不仅仅是一个内容来源于 Stack Overflow，并遵循CC BY-SA 3.0许可协议进行翻译与使用

• 回答 (1)
• 关注 (0)
• 查看 (9)

``````class Sudoku_Checker:
def __init__(self,board):
self.board = board

def board_validater(self,board):
self.checkSquares(board)

return self.checkRows(board) == True and self.checkCols(board) == True and self.checkSquares(board) == True

def checkSquares(self,board):
compare = [1,2,3,4,5,6,7,8,9]
hold = []
row = 0
column = 0
for i in range(row, row + 3):
for j in range(column, column + 3):
hold.append(board[i][j])
if len(hold)==9:
if sorted(hold) == compare:
hold =[]
continue
else:
return False

return True

board = [  [5, 3, 4, 6, 7, 8, 9, 1, 2],
[6, 7, 2, 1, 9, 5, 3, 4, 8],
[1, 9, 8, 3, 4, 2, 5, 6, 0],
[8, 5, 9, 7, 6, 1, 4, 2, 3],
[4, 2, 6, 8, 5, 3, 7, 9, 1],
[7, 1, 3, 9, 2, 4, 8, 5, 6],
[9, 6, 1, 5, 3, 7, 2, 8, 4],
[2, 8, 7, 4, 1, 9, 6, 3, 5],
[3, 4, 5, 2, 8, 6, 1, 7, 9]]
s = Sudoku_Checker(board)
s.board_validater(board)

``````

### 1 个回答

``````board = np.array(board)
compare = np.arange(9)

for i in range(9):
subgrid = board[i//3:i//3+3,i%3:i%3].flatten()
if not np.array_equal(subgrid,compare):
return False
``````