标签:
Problem Definition:
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.
The Sudoku board could be partially filled, where empty cells are filled with the character ‘.‘
.
A partially filled sudoku which is valid.
Note:
A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.
Solution: 用仨字典,一次扫描,针对n x n 的Sudoku,时间复杂度O(n^2).
1 def isValidSudoku( board ): 2 col,row,block={},{},{} 3 for i in range(9): 4 for j in range(9): 5 if board[i][j]==‘.‘: 6 continue 7 d=board[i][j] 8 T=row.get(str(i)+d,False) 9 if T: 10 return False 11 else: 12 row[str(i)+d]=True 13 T=col.get(str(j)+d,False) 14 if T: 15 return False 16 else: 17 col[str(j)+d]=True 18 T=block.get(str((i/3)*3+j/3)+d,False) 19 if T: 20 return False 21 else: 22 block[str((i/3)*3+j/3)+d]=True 23 return True
标签:
原文地址:http://www.cnblogs.com/acetseng/p/4674443.html