forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path2.py
More file actions
64 lines (56 loc) Β· 1.93 KB
/
2.py
File metadata and controls
64 lines (56 loc) Β· 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# BOJμμλ [μΈμ΄]λ₯Ό PyPy3λ‘ μ€μ νμ¬ μ μΆν΄μ£ΌμΈμ.
n, m = map(int, input().split())
data = [] # μ΄κΈ° λ§΅ 리μ€νΈ
temp = [[0] * m for _ in range(n)] # λ²½μ μ€μΉν λ€μ λ§΅ 리μ€νΈ
for _ in range(n):
data.append(list(map(int, input().split())))
# 4κ°μ§ μ΄λ λ°©ν₯μ λν 리μ€νΈ
dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]
result = 0
# κΉμ΄ μ°μ νμ(DFS)μ μ΄μ©ν΄ κ° λ°μ΄λ¬μ€κ° μ¬λ°©μΌλ‘ νΌμ§λλ‘ νκΈ°
def virus(x, y):
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
# μ, ν, μ’, μ° μ€μμ λ°μ΄λ¬μ€κ° νΌμ§ μ μλ κ²½μ°
if nx >= 0 and nx < n and ny >= 0 and ny < m:
if temp[nx][ny] == 0:
# ν΄λΉ μμΉμ λ°μ΄λ¬μ€ λ°°μΉνκ³ , λ€μ μ¬κ·μ μΌλ‘ μν
temp[nx][ny] = 2
virus(nx, ny)
# νμ¬ λ§΅μμ μμ μμμ ν¬κΈ° κ³μ°νλ λ©μλ
def get_score():
score = 0
for i in range(n):
for j in range(m):
if temp[i][j] == 0:
score += 1
return score
# κΉμ΄ μ°μ νμ(DFS)μ μ΄μ©ν΄ μΈν리λ₯Ό μ€μΉνλ©΄μ, λ§€ λ² μμ μμμ ν¬κΈ° κ³μ°
def dfs(count):
global result
# μΈνλ¦¬κ° 3κ° μ€μΉλ κ²½μ°
if count == 3:
for i in range(n):
for j in range(m):
temp[i][j] = data[i][j]
# κ° λ°μ΄λ¬μ€μ μμΉμμ μ ν μ§ν
for i in range(n):
for j in range(m):
if temp[i][j] == 2:
virus(i, j)
# μμ μμμ μ΅λκ° κ³μ°
result = max(result, get_score())
return
# λΉ κ³΅κ°μ μΈν리λ₯Ό μ€μΉ
for i in range(n):
for j in range(m):
if data[i][j] == 0:
data[i][j] = 1
count += 1
dfs(count)
data[i][j] = 0
count -= 1
dfs(0)
print(result)