Skip to content

Commit ef6de39

Browse files
committed
백준 2239번 스도쿠 v2
문제 링크: https://www.acmicpc.net/problem/2239
1 parent f60756f commit ef6de39

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed

백준 2239번 스도쿠 v2/main.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import sys
2+
3+
4+
def solve():
5+
input = sys.stdin.readline
6+
board = []
7+
empties = []
8+
9+
for idx_row in range(9):
10+
row = input().strip()
11+
board.append([0 for _ in range(9)])
12+
for idx_col in range(9):
13+
board[idx_row][idx_col] = int(row[idx_col])
14+
15+
if board[idx_row][idx_col] == 0:
16+
empties.append([idx_row, idx_col])
17+
18+
rec(board, 0, empties)
19+
20+
for idx_row in range(9):
21+
print(''.join(list(map(str, board[idx_row]))))
22+
23+
24+
def rec(board, idx_empty, empties):
25+
if len(empties) == idx_empty:
26+
return True
27+
28+
idx_row, idx_col = empties[idx_empty]
29+
30+
for number in range(1, 10):
31+
if False == can_place_row(board, idx_row, number):
32+
continue
33+
34+
if False == can_place_col(board, idx_col, number):
35+
continue
36+
37+
if False == can_place_3x3(board, idx_row, idx_col, number):
38+
continue
39+
40+
board[idx_row][idx_col] = number
41+
if rec(board, idx_empty + 1, empties):
42+
return True
43+
board[idx_row][idx_col] = 0
44+
45+
return False
46+
47+
48+
def can_place_row(board, idx_row, number) -> bool:
49+
return number not in board[idx_row]
50+
51+
52+
def can_place_col(board, idx_col, number) -> bool:
53+
return number not in [board[i][idx_col] for i in range(9)]
54+
55+
56+
def can_place_3x3(board, idx_row, idx_col, number) -> bool:
57+
row = (idx_row // 3) * 3
58+
col = (idx_col // 3) * 3
59+
60+
for i in range(row, row + 3):
61+
for j in range(col, col + 3):
62+
if board[i][j] == number:
63+
return False
64+
65+
return True
66+
67+
68+
if __name__ == '__main__':
69+
solve()

백준 2239번 스도쿠 v2/test1.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
103000509
2+
002109400
3+
000704000
4+
300502006
5+
060000050
6+
700803004
7+
000401000
8+
009205800
9+
804000107
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
143628579
2+
572139468
3+
986754231
4+
391542786
5+
468917352
6+
725863914
7+
237481695
8+
619275843
9+
854396127
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import sys
2+
from pathlib import Path
3+
from unittest import TestCase
4+
from main import solve
5+
6+
7+
class Test(TestCase):
8+
def my_solve(self, testcase_input):
9+
sys.stdin = open(testcase_input, 'r')
10+
stdout = sys.stdout
11+
sys.stdout = open('stdout.txt', 'w')
12+
solve()
13+
sys.stdout.close()
14+
sys.stdout = stdout
15+
16+
def test_solve(self, testcase_number: str):
17+
self.my_solve('test' + testcase_number + '.txt')
18+
self.assertEqual(
19+
Path('test' + testcase_number + '_answer.txt').read_text().strip(),
20+
Path('stdout.txt').read_text().strip())
21+
22+
def test1_solve(self):
23+
self.test_solve('1')

0 commit comments

Comments
 (0)