728x90
어느 날 스도쿠 300 문제가 담겨있는 책을 풀던 저는 귀찮음에 스도쿠 괴물을 만들고 말았습니다 어떤 스도쿠이든지 기본 룰 스도쿠는 빛의 속도로 풀어내는 괴물입니다
def find_empty_cell(board):
for row in range(9):
for col in range(9):
if board[row][col] == 0:
return row, col
return None
def is_valid(board, row, col, num):
for i in range(9):
if board[row][i] == num:
return False
for i in range(9):
if board[i][col] == num:
return False
start_row = (row // 3) * 3
start_col = (col // 3) * 3
for i in range(3):
for j in range(3):
if board[start_row + i][start_col + j] == num:
return False
return True
def solve_sudoku(board):
empty_cell = find_empty_cell(board)
if not empty_cell:
return True
row, col = empty_cell
for num in range(1, 10):
if is_valid(board, row, col, num):
board[row][col] = num
if solve_sudoku(board):
return True
board[row][col] = 0
return False
board = [
[5, 3, 0, 0, 7, 0, 0, 0, 0],
[6, 0, 0, 1, 9, 5, 0, 0, 0],
[0, 9, 8, 0, 0, 0, 0, 6, 0],
[8, 0, 0, 0, 6, 0, 0, 0, 3],
[4, 0, 0, 8, 0, 3, 0, 0, 1],
[7, 0, 0, 0, 2, 0, 0, 0, 6],
[0, 6, 0, 0, 0, 0, 2, 8, 0],
[0, 0, 0, 4, 1, 9, 0, 0, 5],
[0, 0, 0, 0, 8, 0, 0, 7, 9]
]
board1 = [
[0, 0, 0, 0, 0, 0, 0, 1, 0],
[4, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 2, 0, 5, 0, 0, 0, 0, 8],
[0, 0, 0, 2, 0, 0, 0, 0, 6],
[0, 0, 0, 0, 0, 3, 0, 0, 0],
[3, 0, 0, 0, 0, 0, 4, 0, 7],
[0, 0, 5, 0, 0, 0, 0, 7, 3],
[0, 0, 0, 0, 8, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 6, 0, 0, 0]
]
board2 = [
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 7, 0, 0, 0, 0, 3, 0, 9],
[3, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 6, 0, 0, 0, 0, 0, 3, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 2, 0, 0],
[8, 0, 1, 0, 0, 0, 0, 4, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0]
]
boards = [board, board1, board2]
for i in range(0, 3):
for row in boards[i]:
print(row)
print("raw board")
print("\n")
if solve_sudoku(boards[i]):
for row in boards[i]:
print(row)
print("tamtamburin")
print("\n")
else:
print("No solution exists")
728x90
'호그와트' 카테고리의 다른 글
AI가 졸린지 아닌지 판별할 수 있는 건에 관하여 (0) | 2024.06.23 |
---|---|
Where is caesar ? (0) | 2024.06.12 |
tryhackme anthem (0) | 2024.06.02 |
npc 님들 이건 기본이에요 (0) | 2024.06.01 |
타비의 싹싹김치는 오늘도 참새를 부른다 (0) | 2024.05.30 |