-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import sys | ||
|
||
def input(): return sys.stdin.readline().rstrip() | ||
|
||
board = [] | ||
|
||
for _ in range(9): board.append(list(input())) | ||
|
||
def dfs(board, now_row, now_col): | ||
|
||
# ์ด๋ฏธ ๋ค์ด์๋ ์นธ์ผ ๊ฒฝ์ฐ | ||
if board[now_row][now_col] != "0": | ||
new_row = now_row | ||
new_col = now_col + 1 | ||
|
||
if new_col >= 9: | ||
new_col = 0 | ||
new_row += 1 | ||
|
||
# ์ค๋์ฟ ์์ฑ | ||
if new_row >= 9: return board | ||
|
||
temp = dfs(board, new_row, new_col) | ||
|
||
if temp is not None: return temp | ||
|
||
# ๋น์ด์๋ ์นธ์ผ ๊ฒฝ์ฐ | ||
else: | ||
need_number = { str(x) for x in range(1,10) } | ||
|
||
# ๊ฐ๋ก ํ ๊ฒ์ฌ | ||
for col in range(9): | ||
if board[now_row][col] != "0": | ||
need_number.discard(board[now_row][col]) | ||
|
||
# ์ธ๋ก ํ ๊ฒ์ฌ | ||
for row in range(9): | ||
if board[row][now_col] != "0": | ||
need_number.discard(board[row][now_col]) | ||
|
||
# 3X3 ๊ฒ์ฌ | ||
temp_row = (now_row//3)*3 | ||
temp_col = (now_col//3)*3 | ||
for inner_row in range(temp_row, temp_row+3): | ||
for inner_col in range(temp_col, temp_col+3): | ||
if board[inner_row][inner_col] != "0": | ||
need_number.discard(board[inner_row][inner_col]) | ||
|
||
# ๋ง์ฝ ๋ฃ์ ์ ์๋๊ฒ์ด ์์ผ๋ฉด None์ ๋ฆฌํด | ||
if len(need_number) == 0: return None | ||
|
||
need_number = sorted(list(map(int,need_number))) | ||
|
||
for dedicate_number in need_number: | ||
board[now_row][now_col] = str(dedicate_number) | ||
|
||
new_row = now_row | ||
new_col = now_col + 1 | ||
|
||
if new_col >= 9: | ||
new_col = 0 | ||
new_row += 1 | ||
|
||
# ์ค๋์ฟ ์์ฑ | ||
if new_row >= 9: return board | ||
|
||
temp = dfs(board, new_row, new_col) | ||
|
||
if temp is not None: return temp | ||
|
||
board[now_row][now_col] = "0" | ||
|
||
return None | ||
|
||
answer = dfs(board, 0, 0) | ||
|
||
for row in answer: | ||
print("".join(row)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters