ধরুন আমাদের একটি 9x9 সুডোকু বোর্ড আছে। আমাদের যাচাই করতে হবে সেটা বৈধ নাকি এখন। শুধুমাত্র ভরা কক্ষগুলিকে নিম্নলিখিত নিয়ম অনুসারে যাচাই করতে হবে -
- প্রতিটি সারিতে পুনরাবৃত্তি ছাড়াই 1-9 পর্যন্ত সংখ্যা থাকতে হবে।
- প্রতিটি কলামে পুনরাবৃত্তি ছাড়াই 1-9 পর্যন্ত সংখ্যা থাকতে হবে।
- গ্রিডের 9টি (3x3) সাব-বাক্সের প্রতিটিতে 1-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 |
এটি বৈধ৷
৷এটি সমাধান করতে, আমরা এই পদক্ষেপগুলি অনুসরণ করব -
- আমি 0 থেকে 8
- পরিসরে
- রো, কোল এবং ব্লক, রো_কিউব :=3 * (i / 3), এবং col_cube :=3 * (i mod 3) নামে কিছু খালি অভিধান তৈরি করুন 0 থেকে 8 এর মধ্যে j-এর জন্য
- যদি বোর্ড[i, j] ফাঁকা না হয় এবং বোর্ড[i, j] সারি হয়, তাহলে মিথ্যা দিন
- সারি[বোর্ড[i, j]] :=1
- যদি বোর্ড[j, i] ফাঁকা না হয় এবং বোর্ড [j, i] কলে থাকে, তাহলে মিথ্যা দিন
- col[board[j, i]] :=1
- rc :=row_cube + j/3 এবং cc :=col_cube + j মোড 3
- যদি বোর্ড [rc, cc] ব্লক এবং বোর্ডে [rc, cc] ফাঁকা না হয়, তাহলে মিথ্যা দিন
- block[board[rc, cc]] :=1
উদাহরণ(পাইথন)
আসুন আরও ভালোভাবে বোঝার জন্য নিচের বাস্তবায়ন দেখি −
class Solution(object): def isValidSudoku(self, board): """ :type board: List[List[str]] :rtype: bool """ 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