我该如何使这三重数独用于循环工作

时间:2019-03-15 20:05:11

标签: python python-3.x

我在使用此算法时遇到了一些麻烦。我只能抓一个子网格。我只包括了子网格部分,因为我自己完成了checkRows和checkColumns。如何连接此代码以捕获其余代码?非常感谢您的帮助。谢谢。

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

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

    return  self.checkSquares(board) == True

  def checkSquares(self,board):
      compare = [1,2,3,4,5,6,7,8,9]
      hold = []
      row = 0
      column = 0
      square = 0
      for p in range(square, square+3):
          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
          square +=3
          row += 3


      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)

2 个答案:

答案 0 :(得分:0)

所以在我的变量linecolumn中,我得到了eaxh 3x3矩阵的左上角,然后从那里迭代以构造正方形。

请问这是您要寻找的东西。

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]
]

for line in range(0, 9, 3):
    for column in range(0, 9, 3):
        square = [[board[l][c] for c in range(column, column + 3)] for l in range(line, line + 3)]
        print(square)

结果:

[[5, 3, 4], [6, 7, 2], [1, 9, 8]]
[[6, 7, 8], [1, 9, 5], [3, 4, 2]]
[[9, 1, 2], [3, 4, 8], [5, 6, 0]]
[[8, 5, 9], [4, 2, 6], [7, 1, 3]]
[[7, 6, 1], [8, 5, 3], [9, 2, 4]]
[[4, 2, 3], [7, 9, 1], [8, 5, 6]]
[[9, 6, 1], [2, 8, 7], [3, 4, 5]]
[[5, 3, 7], [4, 1, 9], [2, 8, 6]]
[[2, 8, 4], [6, 3, 5], [1, 7, 9]]

答案 1 :(得分:0)

希望有人发现这很有用。我更喜欢这种实现方式,因为它对我来说更具可读性和可理解性。另一个评论者也有一个很好的解决方案,除了列表理解很麻烦。

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

  def board_validater(self,board):
    return self.checkSquares(board) == True

  def checkSquares(self,board):
    compare = range(1,10)
    for i in range(0, 9, 3):
        for j in range(0, 9, 3):
            nums = board[i][j:j+3] + board[i+1][j:j+3] + board[i+2][j:j+3]
            if sorted(nums) == compare:
                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)