1. The topic
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
.
The general meaning of the title is that the rules of Sudoku, each row 1~9 is not repeated, each column 1~9 cannot be repeated, and the 3x3 small squares 1~9 are not repeated.
2. Ideas
This problem can be used to judge whether the numbers in the rows, columns and small squares are repeated. You can use the hash idea. In fact, the dictionary in python is the same. Iterate through the rows, columns and small squares one by one, and store them when encountering different values. If the stored procedure encounters the same, return False.
line number rule
Observe the line number rule:
The 0th nine-square grid: 000111222; the first nine-square grid: 000111222; the second nine-square grid: 000111222;
The third nine-square grid: 333444555; the fourth nine-square grid: 333444555; the fifth nine-square grid: 333444555;
The sixth nine-square grid: 666777888; the seventh nine-square grid: 666777888; the eighth nine-square grid: 666777888;
It can be seen that the row number increases by 3 for every three nine-square grids; for a single nine-square grid, the row number for every three grid points increases by 1.
Therefore, the row number of the j-th grid point of the i-th nine-square grid can be expressed as i/3*3+j/3
3. Code
#coding:utf-8 ''' Solution0 is the most primitive violent solution, but the code is redundant ''' class Solution0: def isValidSudoku(self, board): """ :type board: List[List[str]] :rtype: bool """ for row in range(9): if not self.isValidNine(board[row]): return False column = [c[row] for c in board] if not self.isValidNine(column): print('False') return False for i in [0,3,6]: for j in [0,3,6]: block = [board[s][t] for s in [i,i+1,i+2] for t in [j,j+1,j+2]] if not self.isValidNine(block): print(False) return False print('True') return True def isValidNine(self,row): """ Are the current nine numbers valid? :param row:int :return:bool """ map = {} for c in row: if c != '.': if c in map: return False else: map[c] = True #If there is no c in the dictionary map, save c to the map dictionary, the key is c, and the value is True return True ''' Solution1 simplifies the code and uses three matrices to check whether the three rules have repeated numbers ''' class Solution1: def isValidSudoku(self, board): """ :type board: List[List[str]] :rtype: bool """ row = [[False for i in range(9)] for j in range(9)] col = [[False for i in range(9)] for j in range(9)] block = [[False for i in range(9)] for j in range(9)] for i in range(9): for j in range(9): if board[i][j] != '.': num = int(board[i][j])-1 #In fact, the hashing idea is used here, because the numbers are between 0 and 9, so the index is reduced by 1, just imagine if the number in a certain position and other positions If the numbers are the same, the num value is also the same, so through True or False, you can identify whether there are repetitions k = i//3*3 + j//3 #K represents the row number of the j-th grid point of the i-th nine-square grid if row[i][num] or col[j][num] or block[k][num]: print('False') return False row[i][num] = col[j][num] = block[k][num] = True print('True') return True if __name__ == '__main__': 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"] ] #ss =Solution0() ss = Solution1() ss.isValidSudoku(board)
Reference blog: https://www.cnblogs.com/ganganloveu/p/4170632.html https://blog.csdn.net/coder_orz/article/details/51596499