Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

42-tgyuuAn #152

Merged
merged 2 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions tgyuuAn/DFS/์Šค๋„์ฟ .py
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))
1 change: 1 addition & 0 deletions tgyuuAn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@
| 38์ฐจ์‹œ | 2023.02.15 | DP | <a href="https://www.acmicpc.net/problem/2749">ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜ 3</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/137
| 39์ฐจ์‹œ | 2023.02.18 | DP | <a href="https://www.acmicpc.net/problem/7579">์•ฑ</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/139
| 40์ฐจ์‹œ | 2023.02.21 | DP | <a href="https://www.acmicpc.net/problem/31413">์ž…๋Œ€</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/142
| 42์ฐจ์‹œ | 2023.03.07 | DFS | <a href="https://www.acmicpc.net/problem/2239">์Šค๋„์ฟ </a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/152
---