मान लीजिए हमारे पास एक 9×9 सुडोकू ग्रिड है। हमें यह देखना होगा कि यह वैध है या अभी। केवल भरे हुए कक्षों को निम्नलिखित नियमों के अनुसार सत्यापित करने की आवश्यकता है -
-
प्रत्येक पंक्ति में बिना दोहराव के 1−9 के अंक होने चाहिए।
-
प्रत्येक कॉलम में दोहराव के बिना 1−9 के अंक होने चाहिए।
-
ग्रिड के 9 (3−3) सब-बॉक्स में से प्रत्येक में बिना दोहराव के 1−9 के अंक होने चाहिए।
मान लीजिए सुडोकू ग्रिड इस तरह है -
यह मान्य है।
इसे हल करने के लिए, हम इन चरणों का पालन करेंगे -
-
मेरे लिए 0 से 8 की सीमा में
-
पंक्ति, कॉल और ब्लॉक नामक कुछ खाली शब्दकोश बनाएँ, row_cube:=3 * (i / 3), और col_cube:=3 * (i mod 3)
-
j के लिए 0 से 8 की सीमा में
-
अगर बोर्ड [i, j] खाली नहीं है और बोर्ड [i, j] पंक्ति में है, तो झूठी वापसी करें
-
पंक्ति [बोर्ड[i, j]] :=1
-
अगर बोर्ड [जे, आई] खाली नहीं है और बोर्ड [जे, आई] कॉलम में है, तो झूठी वापसी करें
-
कर्नल [बोर्ड [जे, आई]]:=1
-
आरसी :=row_cube + j/3 और cc :=col_cube + j mod 3
-
अगर ब्लॉक और बोर्ड में बोर्ड [आरसी, सीसी] [आरसी, सीसी] खाली नहीं है, तो झूठी वापसी करें
-
ब्लॉक [बोर्ड [आरसी, सीसी]]:=1
-
-
-
सही लौटें
आइए बेहतर समझ पाने के लिए निम्नलिखित कार्यान्वयन देखें -
उदाहरण
class Solution(object): def isValidSudoku(self, board): for i in range(9): row = {} column = {} block = {} row_cube = 3 * (i//3) column_cube = 3 * (i%3) for j in range(9): if board[i][j]!='.' and board[i][j] in row: return False row[board[i][j]] = 1 if board[j][i]!='.' and board[j][i] in column: return False column[board[j][i]] = 1 rc= row_cube+j//3 cc = column_cube + j%3 if board[rc][cc] in block and board[rc][cc]!='.': return False block[board[rc][cc]]=1 return True ob1 = Solution() print(ob1.isValidSudoku([ ["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"]]))
इनपुट
[ ["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"]]
आउटपुट
True