Skip to content

Commit d50b42d

Browse files
committed
solved(python): baekjoon 2206
1 parent a597b9f commit d50b42d

4 files changed

Lines changed: 103 additions & 0 deletions

File tree

baekjoon/python/2206/__init__.py

Whitespace-only changes.

baekjoon/python/2206/main.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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, read())) for _ in range(self.n)]
11+
12+
def solve(self) -> None:
13+
print(self.dijkstra())
14+
15+
def dijkstra(self) -> int:
16+
queue, visited = deque([((0, 0), 1, True)]), {(True, 0, 0)}
17+
18+
while queue:
19+
(x, y), depth, can_break = queue.popleft()
20+
if (x, y) == (self.m - 1, self.n - 1):
21+
return depth
22+
23+
for nx, ny in [(x + dx, y + dy) for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]]:
24+
if not (0 <= nx < self.m and 0 <= ny < self.n):
25+
continue
26+
27+
if self.matrix[ny][nx] == 0 and (can_break, nx, ny) not in visited:
28+
visited.add((can_break, nx, ny))
29+
queue.append(((nx, ny), depth + 1, can_break))
30+
31+
if self.matrix[ny][nx] == 1 and can_break and (False, nx, ny) not in visited:
32+
visited.add((False, nx, ny))
33+
queue.append(((nx, ny), depth + 1, False))
34+
35+
return -1
36+
37+
38+
if __name__ == "__main__":
39+
Problem().solve()

baekjoon/python/2206/sample.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
[
2+
{
3+
"input": [
4+
"6 4",
5+
"0100",
6+
"1110",
7+
"1000",
8+
"0000",
9+
"0111",
10+
"0000"
11+
],
12+
"expected": [
13+
"15"
14+
]
15+
},
16+
{
17+
"input": [
18+
"4 4",
19+
"0111",
20+
"1111",
21+
"1111",
22+
"1110"
23+
],
24+
"expected": [
25+
"-1"
26+
]
27+
}
28+
]

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