2025-12-08 21:01:01 +09:00

49 lines
1.3 KiB
Python

import sys
input = sys.stdin.readline
board = []
emptyAxis = []
row_check = [[False]*10 for _ in range(9)]
col_check = [[False]*10 for _ in range(9)]
square_check = [[False]*10 for _ in range(9)]
def square_idx(r, c) :
return (r//3)*3 + c//3
def backTracking(depth) :
if depth == len(emptyAxis) :
for row in board :
print("".join(map(str, row)))
exit(0)
row, col = emptyAxis[depth]
sqr_idx = square_idx(row, col)
for num in range(1, 10) :
if not row_check[row][num] and not col_check[col][num] and not square_check[sqr_idx][num] :
board[row][col] = num
row_check[row][num] = True
col_check[col][num] = True
square_check[sqr_idx][num] = True
backTracking(depth+1)
board[row][col] = 0
row_check[row][num] = False
col_check[col][num] = False
square_check[square_idx(row, col)][num] = False
for i in range(9) :
row = list(map(int, list(input().strip())))
board.append(row)
for j in range(9) :
num = row[j]
if num == 0 :
emptyAxis.append((i,j))
else :
row_check[i][num] = True
col_check[j][num] = True
square_check[square_idx(i,j)][num] = True
backTracking(0)