Skip to content

Commit 6dcac81

Browse files
committed
solved(python): baekjoon 16928
1 parent 0560cd5 commit 6dcac81

File tree

4 files changed

+119
-0
lines changed

4 files changed

+119
-0
lines changed

baekjoon/python/16928/__init__.py

Whitespace-only changes.

baekjoon/python/16928/main.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import sys
2+
from collections import deque, defaultdict
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+
11+
self.moves = defaultdict(int)
12+
for start, end in [map(int, read().split()) for _ in range(self.n + self.m)]:
13+
self.moves[start] = end
14+
15+
def solve(self) -> None:
16+
print(self.bfs())
17+
18+
def bfs(self) -> int:
19+
queue, board = deque([1]), [0] * 101
20+
21+
while queue:
22+
pos = queue.popleft()
23+
if pos == 100:
24+
return board[pos]
25+
26+
for dice in range(1, 7):
27+
next_pos = pos + dice
28+
if next_pos > 100:
29+
continue
30+
31+
if next_pos in self.moves:
32+
next_pos = self.moves[next_pos]
33+
34+
if board[next_pos] == 0:
35+
board[next_pos] = board[pos] + 1
36+
queue.append(next_pos)
37+
38+
return -1
39+
40+
41+
if __name__ == "__main__":
42+
Problem().solve()

baekjoon/python/16928/sample.json

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
[
2+
{
3+
"input": [
4+
"3 7",
5+
"32 62",
6+
"42 68",
7+
"12 98",
8+
"95 13",
9+
"97 25",
10+
"93 37",
11+
"79 27",
12+
"75 19",
13+
"49 47",
14+
"67 17"
15+
],
16+
"expected": [
17+
"3"
18+
]
19+
},
20+
{
21+
"input": [
22+
"4 9",
23+
"8 52",
24+
"6 80",
25+
"26 42",
26+
"2 72",
27+
"51 19",
28+
"39 11",
29+
"37 29",
30+
"81 3",
31+
"59 5",
32+
"79 23",
33+
"53 7",
34+
"43 33",
35+
"77 21"
36+
],
37+
"expected": [
38+
"5"
39+
]
40+
}
41+
]

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