Skip to content

Commit 10fbae4

Browse files
committed
solved(python): baekjoon 16236
1 parent 31e9e8c commit 10fbae4

4 files changed

Lines changed: 199 additions & 0 deletions

File tree

baekjoon/python/16236/__init__.py

Whitespace-only changes.

baekjoon/python/16236/main.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import heapq
2+
import sys
3+
4+
read = lambda: sys.stdin.readline().rstrip()
5+
6+
7+
class Problem:
8+
def __init__(self):
9+
self.n = int(read())
10+
self.graph = [list(map(int, read().split())) for _ in range(self.n)]
11+
12+
def solve(self) -> None:
13+
graph = self.graph
14+
shark = self.find_shark(graph)
15+
16+
shark_size, weight, time = 2, 0, 0
17+
while True:
18+
found = self.find_nearest_fish(
19+
graph,
20+
shark,
21+
shark_size,
22+
)
23+
if not found:
24+
break
25+
26+
(y, x), append_time = found
27+
28+
weight += 1
29+
if weight == shark_size:
30+
shark_size += 1
31+
weight = 0
32+
33+
time += append_time
34+
graph[shark[0]][shark[1]] = 0
35+
shark = (y, x)
36+
graph[shark[0]][shark[1]] = 9
37+
38+
print(time)
39+
40+
def find_shark(
41+
self,
42+
graph: list[list[int]],
43+
) -> tuple[int, int]:
44+
for row in range(self.n):
45+
for col in range(self.n):
46+
if graph[row][col] == 9:
47+
return row, col
48+
49+
def find_nearest_fish(
50+
self,
51+
graph: list[list[int]],
52+
shark: tuple[int, int],
53+
shark_size: int,
54+
) -> tuple[tuple[int, int], int] | None:
55+
queue, visited, found = [(shark, 0)], {shark}, []
56+
while queue and not found:
57+
candidates = []
58+
for (y, x), time in queue:
59+
for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
60+
if (
61+
(0 <= x + dx < self.n and 0 <= y + dy < self.n)
62+
and graph[y + dy][x + dx] <= shark_size
63+
and (
64+
y + dy,
65+
x + dx,
66+
)
67+
not in visited
68+
):
69+
candidates.append(((y + dy, x + dx), time + 1))
70+
visited.add((y + dy, x + dx))
71+
if 0 < graph[y + dy][x + dx] < shark_size:
72+
heapq.heappush(
73+
found, ((y + dy, x + dx), ((y + dy, x + dx), time + 1))
74+
)
75+
76+
queue = candidates
77+
78+
if found:
79+
return heapq.heappop(found)[1]
80+
81+
return None
82+
83+
84+
if __name__ == "__main__":
85+
Problem().solve()

baekjoon/python/16236/sample.json

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
[
2+
{
3+
"input": [
4+
"3",
5+
"0 0 0",
6+
"0 0 0",
7+
"0 9 0"
8+
],
9+
"expected": [
10+
"0"
11+
]
12+
},
13+
{
14+
"input": [
15+
"3",
16+
"0 0 1",
17+
"0 0 0",
18+
"0 9 0"
19+
],
20+
"expected": [
21+
"3"
22+
]
23+
},
24+
{
25+
"input": [
26+
"4",
27+
"4 3 2 1",
28+
"0 0 0 0",
29+
"0 0 9 0",
30+
"1 2 3 4"
31+
],
32+
"expected": [
33+
"14"
34+
]
35+
},
36+
{
37+
"input": [
38+
"6",
39+
"5 4 3 2 3 4",
40+
"4 3 2 3 4 5",
41+
"3 2 9 5 6 6",
42+
"2 1 2 3 4 5",
43+
"3 2 1 6 5 4",
44+
"6 6 6 6 6 6"
45+
],
46+
"expected": [
47+
"60"
48+
]
49+
},
50+
{
51+
"input": [
52+
"6",
53+
"6 0 6 0 6 1",
54+
"0 0 0 0 0 2",
55+
"2 3 4 5 6 6",
56+
"0 0 0 0 0 2",
57+
"0 2 0 0 0 0",
58+
"3 9 3 0 0 1"
59+
],
60+
"expected": [
61+
"48"
62+
]
63+
},
64+
{
65+
"input": [
66+
"6",
67+
"1 1 1 1 1 1",
68+
"2 2 6 2 2 3",
69+
"2 2 5 2 2 3",
70+
"2 2 2 4 6 3",
71+
"0 0 0 0 0 6",
72+
"0 0 0 0 0 9"
73+
],
74+
"expected": [
75+
"39"
76+
]
77+
}
78+
]

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