Skip to content

Commit 9c00c3c

Browse files
committed
solve(BOJ): G4_14502_연구소_py
1 parent 6630060 commit 9c00c3c

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

src/boj/G4_14502_연구소.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# https://www.acmicpc.net/problem/14502
2+
# 11:16~12:30, 7:36~7:55
3+
from collections import deque
4+
5+
6+
def main():
7+
n, m = map(int, input().split())
8+
board = [list(map(int, input().split())) for _ in range(n)]
9+
10+
virus = []
11+
s = deque()
12+
res = 0
13+
off = ((1, 0), (-1, 0), (0, 1), (0, -1))
14+
wall_count = 0
15+
min_v_count = 1000
16+
for i in range(n):
17+
for j in range(m):
18+
if board[i][j] == 2:
19+
virus.append((i, j))
20+
if board[i][j] == 1:
21+
wall_count += 1
22+
23+
mark = 4
24+
for w1 in range(n * m):
25+
if board[w1 // m][w1 % m] in [1, 2]: continue
26+
for w2 in range(w1 + 1, n * m):
27+
if board[w2 // m][w2 % m] in [1, 2]: continue
28+
for w3 in range(w2 + 1, n * m):
29+
if board[w3 // m][w3 % m] in [1, 2]: continue
30+
walls = [(w // m, w % m) for w in [w1, w2, w3]]
31+
for w in walls:
32+
board[w[0]][w[1]] = 1
33+
34+
q = deque(virus)
35+
36+
v_count = len(virus)
37+
while len(q) > 0:
38+
if v_count > min_v_count:
39+
v_count = 100
40+
break
41+
x, y = q.popleft()
42+
for dx, dy in off:
43+
nx = x + dx
44+
ny = y + dy
45+
if 0 <= nx < n and 0 <= ny < m:
46+
if board[nx][ny] in [1, 2, mark]:
47+
continue
48+
q.append((nx, ny))
49+
board[nx][ny] = mark
50+
v_count += 1
51+
52+
mark += 1
53+
54+
zeros = n * m - wall_count - 3 - v_count
55+
res = max(res, zeros)
56+
for w in walls:
57+
board[w[0]][w[1]] = 0
58+
59+
print(res)
60+
61+
62+
main()

0 commit comments

Comments
 (0)