A cool algorithm to check a Sudoku field?

前端 未结 25 756
清酒与你
清酒与你 2021-01-30 09:19

Does anyone know a simple algorithm to check if a Sudoku-Configuration is valid? The simplest algorithm I came up with is (for a board of size n) in Pseudocode

f         


        
25条回答
  •  清歌不尽
    2021-01-30 09:59

    def solution(board):
        for i in board:
            if sum(i) != 45:
                return "Incorrect"
    
        for i in range(9):
            temp2 = []
            for x in range(9):
                temp2.append(board[i][x])
    
            if sum(temp2) != 45:
                return "Incorrect"
    
        return "Correct"
    
    board = []
    for i in range(9):
        inp = raw_input()
        temp = [int(i) for i in inp]
        board.append(temp)
    
    print solution(board)
    
    

提交回复
热议问题