Skip to content

Commit 85813db

Browse files
committed
solved(python): programmers
- PCCP 기출문제 / 석유 시추
1 parent ac95991 commit 85813db

4 files changed

Lines changed: 196 additions & 0 deletions

File tree

programmers/PCCP_기출문제/python/석유_시추/__init__.py

Whitespace-only changes.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from collections import deque
2+
from typing import List, Tuple
3+
4+
5+
def solution(land: List[List[int]]) -> int:
6+
n, m = len(land), len(land[0])
7+
graph = find_graph(n, m, land)
8+
9+
maximum = 0
10+
for col in range(m):
11+
amount, visited = 0, set()
12+
13+
for row in range(n):
14+
group_id, value = graph[row][col]
15+
if group_id != 0 and group_id not in visited:
16+
visited.add(group_id)
17+
amount += value
18+
19+
maximum = max(maximum, amount)
20+
21+
return maximum
22+
23+
24+
def find_graph(n: int, m: int, land: List[List[int]]) -> List[List[Tuple[int, int]]]:
25+
graph, visited, count = [[(0, 0) for _ in range(m)] for _ in range(n)], set(), 0
26+
27+
for y in range(n):
28+
for x in range(m):
29+
if land[y][x] == 1 and (x, y) not in visited:
30+
stack, cached, count = deque([(x, y)]), set(), count + 1
31+
32+
while stack:
33+
c, r = stack.pop()
34+
visited.add((c, r))
35+
cached.add((c, r))
36+
37+
for dx, dy in [(0, -1), (0, 1), (-1, 0), (1, 0)]:
38+
nx, ny = c + dx, r + dy
39+
if (0 <= nx < m) and (0 <= ny < n) and land[ny][nx] == 1 and (nx, ny) not in visited:
40+
stack.append((nx, ny))
41+
42+
group_id, amount = count, len(cached)
43+
for c, r in cached:
44+
graph[r][c] = (group_id, amount)
45+
46+
return graph
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
[
2+
{
3+
"params": [
4+
[
5+
[
6+
0,
7+
0,
8+
0,
9+
1,
10+
1,
11+
1,
12+
0,
13+
0
14+
],
15+
[
16+
0,
17+
0,
18+
0,
19+
0,
20+
1,
21+
1,
22+
0,
23+
0
24+
],
25+
[
26+
1,
27+
1,
28+
0,
29+
0,
30+
0,
31+
1,
32+
1,
33+
0
34+
],
35+
[
36+
1,
37+
1,
38+
1,
39+
0,
40+
0,
41+
0,
42+
0,
43+
0
44+
],
45+
[
46+
1,
47+
1,
48+
1,
49+
0,
50+
0,
51+
0,
52+
1,
53+
1
54+
]
55+
]
56+
],
57+
"expected": 9
58+
},
59+
{
60+
"params": [
61+
[
62+
[
63+
1,
64+
0,
65+
1,
66+
0,
67+
1,
68+
1
69+
],
70+
[
71+
1,
72+
0,
73+
1,
74+
0,
75+
0,
76+
0
77+
],
78+
[
79+
1,
80+
0,
81+
1,
82+
0,
83+
0,
84+
1
85+
],
86+
[
87+
1,
88+
0,
89+
0,
90+
1,
91+
0,
92+
0
93+
],
94+
[
95+
1,
96+
0,
97+
0,
98+
1,
99+
0,
100+
1
101+
],
102+
[
103+
1,
104+
0,
105+
0,
106+
0,
107+
0,
108+
0
109+
],
110+
[
111+
1,
112+
1,
113+
1,
114+
1,
115+
1,
116+
1
117+
]
118+
]
119+
],
120+
"expected": 16
121+
}
122+
]
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import json
2+
import os
3+
import unittest
4+
5+
from parameterized import parameterized
6+
7+
from .main import solution
8+
9+
10+
def load_sample(filename: str):
11+
path = os.path.join(os.path.dirname(os.path.abspath(__file__)), filename)
12+
13+
with open(path, "r") as file:
14+
return [(case["params"], case["expected"]) for case in json.load(file)]
15+
16+
17+
class TestCase(unittest.TestCase):
18+
@parameterized.expand(load_sample("sample.json"))
19+
def test_case(self, params: list, expected: any):
20+
# When
21+
result = solution(*params)
22+
23+
# Then
24+
self.assertEqual(expected, result)
25+
26+
27+
if __name__ == "__main__":
28+
unittest.main()

0 commit comments

Comments
 (0)