Skip to content

Commit 3aafc7b

Browse files
authored
solved(python): baekjoon 1987
1 parent 8778dcb commit 3aafc7b

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed

baekjoon/python/1987/__init__.py

Whitespace-only changes.

baekjoon/python/1987/main.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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.r, self.c = map(int, read().split())
10+
self.matrix = [list(read()) for _ in range(self.r)]
11+
12+
def solve(self) -> None:
13+
print(self.dfs())
14+
15+
def dfs(self) -> int:
16+
stack, cache, max_depth = (
17+
deque([((0, 0), 1, 1 << ord(self.matrix[0][0]) - ord("A"))]),
18+
{row: {col: set() for col in range(self.c)} for row in range(self.r)},
19+
1,
20+
)
21+
cache[0][0].add(1 << ord(self.matrix[0][0]) - ord("A"))
22+
23+
while stack:
24+
(x, y), depth, visited = stack.pop()
25+
if depth > max_depth:
26+
max_depth = depth
27+
28+
if max_depth == min(self.r * self.c, 26):
29+
return max_depth
30+
31+
for dx, dy in [(0, -1), (0, 1), (-1, 0), (1, 0)]:
32+
nx, ny = x + dx, y + dy
33+
34+
if 0 <= nx < self.c and 0 <= ny < self.r:
35+
next_token_code = ord(self.matrix[ny][nx]) - ord("A")
36+
37+
if visited & (1 << next_token_code) == 0:
38+
mask = visited | (1 << next_token_code)
39+
40+
if mask not in cache[ny][nx]:
41+
cache[ny][nx].add(mask)
42+
stack.append(((nx, ny), depth + 1, mask))
43+
44+
return max_depth
45+
46+
47+
if __name__ == "__main__":
48+
Problem().solve()

baekjoon/python/1987/sample.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
[
2+
{
3+
"input": [
4+
"2 4",
5+
"CAAB",
6+
"ADCB"
7+
],
8+
"expected": [
9+
"3"
10+
]
11+
},
12+
{
13+
"input": [
14+
"3 6",
15+
"HFDFFB",
16+
"AJHGDH",
17+
"DGAGEH"
18+
],
19+
"expected": [
20+
"6"
21+
]
22+
},
23+
{
24+
"input": [
25+
"5 5",
26+
"IEFCJ",
27+
"FHFKC",
28+
"FFALF",
29+
"HFGCF",
30+
"HMCHH"
31+
],
32+
"expected": [
33+
"10"
34+
]
35+
}
36+
]

baekjoon/python/1987/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)