Skip to content

Commit 1de66f2

Browse files
committed
solved(python): baekjoon 2618
1 parent a22e5b8 commit 1de66f2

4 files changed

Lines changed: 110 additions & 0 deletions

File tree

baekjoon/python/2618/__init__.py

Whitespace-only changes.

baekjoon/python/2618/main.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import itertools
2+
import math
3+
import sys
4+
5+
read = lambda: sys.stdin.readline().rstrip()
6+
7+
8+
class Problem:
9+
def __init__(self):
10+
self.n, self.w = int(read()), int(read())
11+
self.coords = [(0, 0)]
12+
for _ in range(self.w):
13+
y, x = map(int, read().split())
14+
self.coords.append((x - 1, y - 1))
15+
16+
def solve(self) -> None:
17+
dp, path, parent = (
18+
[[math.inf for _ in range(self.w + 1)] for _ in range(self.w + 1)],
19+
[[0 for _ in range(self.w + 1)] for _ in range(self.w + 1)],
20+
[[(0, 0)] * (self.w + 1) for _ in range(self.w + 1)],
21+
)
22+
dp[0][0] = 0
23+
24+
for x, y in itertools.product(range(self.w + 1), repeat=2):
25+
event = max(x, y) + 1
26+
if event > self.w:
27+
continue
28+
29+
cost_x = dp[x][y] + self.calc_distance((0, 0) if x == 0 else self.coords[x], event)
30+
if cost_x < dp[event][y]:
31+
dp[event][y], path[event][y], parent[event][y] = cost_x, 1, (x, y)
32+
33+
cost_y = dp[x][y] + self.calc_distance((self.n - 1, self.n - 1) if y == 0 else self.coords[y], event)
34+
if cost_y < dp[x][event]:
35+
dp[x][event], path[x][event], parent[x][event] = cost_y, 2, (x, y)
36+
37+
minimum, x, y = math.inf, 0, 0
38+
for idx in range(self.w + 1):
39+
if dp[idx][self.w] < minimum:
40+
minimum, x, y = dp[idx][self.w], idx, self.w
41+
if dp[self.w][idx] < minimum:
42+
minimum, x, y = dp[self.w][idx], self.w, idx
43+
44+
sequence = []
45+
for _ in range(self.w):
46+
sequence.append(path[x][y])
47+
x, y = parent[x][y]
48+
49+
print(minimum)
50+
print(*reversed(sequence), sep="\n")
51+
52+
def calc_distance(self, src: tuple[int, int], dist_idx: int) -> int:
53+
return abs(src[0] - self.coords[dist_idx][0]) + abs(src[1] - self.coords[dist_idx][1])
54+
55+
56+
if __name__ == "__main__":
57+
Problem().solve()

baekjoon/python/2618/sample.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[
2+
{
3+
"input": [
4+
"6",
5+
"3",
6+
"3 5",
7+
"5 5",
8+
"2 3"
9+
],
10+
"expected": [
11+
"9",
12+
"2",
13+
"2",
14+
"1"
15+
]
16+
}
17+
]

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