Skip to content

Commit

Permalink
2024-03-07
Browse files Browse the repository at this point in the history
  • Loading branch information
tgyuuAn committed Mar 7, 2024
1 parent 7e66693 commit 771fb24
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
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
---

0 comments on commit 771fb24

Please sign in to comment.