Skip to content

Commit 60ea0a9

Browse files
committed
solved(python): baekjoon 16946
1 parent 83be537 commit 60ea0a9

4 files changed

Lines changed: 159 additions & 0 deletions

File tree

baekjoon/python/16946/__init__.py

Whitespace-only changes.

baekjoon/python/16946/main.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import sys
2+
from collections import deque
3+
4+
read = lambda: sys.stdin.readline().rstrip()
5+
6+
7+
class Problem:
8+
def __init__(self):
9+
self.n, self.m = map(int, read().split())
10+
self.matrix = [list(map(int, list(read()))) for _ in range(self.n)]
11+
12+
def solve(self) -> None:
13+
for row in self.update_matrix():
14+
print(*row, sep="")
15+
16+
def update_matrix(self) -> list[list[int]]:
17+
for count, walls in self.find_group():
18+
for x, y in walls:
19+
self.matrix[y][x] += count
20+
self.matrix[y][x] %= 10
21+
22+
return self.matrix
23+
24+
def find_group(self) -> list[tuple[int, set[tuple[int, int]]]]:
25+
group, stack, visited = (
26+
[],
27+
deque([(1, {(x, y)}, set()) for x in range(self.m) for y in range(self.n) if self.matrix[y][x] == 0]),
28+
set(),
29+
)
30+
31+
while stack:
32+
count, paths, walls = stack.pop()
33+
if len(paths - visited) != len(paths):
34+
continue
35+
36+
new_path = set()
37+
for x, y in paths:
38+
visited.add((x, y))
39+
40+
for dx, dy in ((0, 1), (0, -1), (1, 0), (-1, 0)):
41+
nx, ny = x + dx, y + dy
42+
if (nx, ny) in visited or not (0 <= nx < self.m and 0 <= ny < self.n):
43+
continue
44+
45+
if self.matrix[ny][nx] == 0:
46+
new_path.add((nx, ny))
47+
else:
48+
walls.add((nx, ny))
49+
50+
if new_path:
51+
stack.append((count + len(new_path), new_path, walls))
52+
else:
53+
group.append((count, walls))
54+
55+
return group
56+
57+
58+
if __name__ == "__main__":
59+
Problem().solve()

baekjoon/python/16946/sample.json

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
[
2+
{
3+
"input": [
4+
"3 3",
5+
"101",
6+
"010",
7+
"101"
8+
],
9+
"expected": [
10+
"303",
11+
"050",
12+
"303"
13+
]
14+
},
15+
{
16+
"input": [
17+
"4 5",
18+
"11001",
19+
"00111",
20+
"01010",
21+
"10101"
22+
],
23+
"expected": [
24+
"46003",
25+
"00732",
26+
"06040",
27+
"50403"
28+
]
29+
},
30+
{
31+
"input": [
32+
"5 5",
33+
"10000",
34+
"10000",
35+
"10000",
36+
"10000",
37+
"11000"
38+
],
39+
"expected": [
40+
"00000",
41+
"00000",
42+
"00000",
43+
"00000",
44+
"10000"
45+
]
46+
},
47+
{
48+
"input": [
49+
"5 5",
50+
"10000",
51+
"10000",
52+
"10000",
53+
"10000",
54+
"10000"
55+
],
56+
"expected": [
57+
"10000",
58+
"10000",
59+
"10000",
60+
"10000",
61+
"10000"
62+
]
63+
}
64+
]

baekjoon/python/16946/test_main.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import json
2+
import os.path
3+
import unittest
4+
from io import StringIO
5+
from unittest.mock import patch
6+
7+
from parameterized import parameterized
8+
9+
from main import Problem
10+
11+
12+
def load_sample(filename: str):
13+
path = os.path.join(os.path.dirname(os.path.abspath(__file__)), filename)
14+
15+
with open(path, "r") as file:
16+
return [(case["input"], case["expected"]) for case in json.load(file)]
17+
18+
19+
class TestCase(unittest.TestCase):
20+
@parameterized.expand(load_sample("sample.json"))
21+
def test_case(self, case: str, expected: list[str]):
22+
# When
23+
with (
24+
patch("sys.stdin.readline", side_effect=case),
25+
patch("sys.stdout", new_callable=StringIO) as output,
26+
):
27+
Problem().solve()
28+
29+
result = output.getvalue().rstrip()
30+
31+
# Then
32+
self.assertEqual("\n".join(expected), result)
33+
34+
35+
if __name__ == "__main__":
36+
unittest.main()

0 commit comments

Comments
 (0)