Skip to content

Commit 6613ac9

Browse files
committed
[BOJ] 연구소 / 골드4 / 60분
https://www.acmicpc.net/problem/14502
1 parent ff8f33e commit 6613ac9

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
from collections import deque
2+
import sys
3+
import copy
4+
5+
# 1. 입력값 받기
6+
# 2. 임의로 벽 3개 세우기 (backtracking 사용)
7+
# 3. bfs로 그래프 탐색하며 바이러스가 있는곳(처음에 2인 영역)부터 시작해서 바이러스 확산시키기
8+
# 4. 바이러스 확산 후 안전영역(0인 영역)을 세고 최대값이면 저장
9+
10+
11+
def bfs():
12+
global arr, virusLocation,N,M, maxSafeAreaCount
13+
14+
copyArr = copy.deepcopy(arr)
15+
16+
dx = [0,0,1,-1]
17+
dy = [1,-1,0,0]
18+
19+
queue = deque(virusLocation)
20+
21+
while queue:
22+
startRow,startCol = queue.popleft()
23+
for i in range(4):
24+
row = startRow + dx[i]
25+
col = startCol + dy[i]
26+
27+
if 0<=row<N and 0<=col<M and copyArr[row][col] == 0:
28+
#바이러스 확산
29+
copyArr[row][col] = 2
30+
#방문처리
31+
queue.append((row,col))
32+
33+
#안전영역 세기
34+
safeAreaCount = 0
35+
for i in range(N):
36+
for j in range(M):
37+
if copyArr[i][j] == 0:
38+
safeAreaCount += 1
39+
40+
#안전영역의 최댓값 비교
41+
maxSafeAreaCount = max(maxSafeAreaCount, safeAreaCount)
42+
43+
44+
def makeWall(arr, numOfWall):
45+
46+
# 세운 벽의 개수가 3개면 바이러스 확산시키기
47+
if numOfWall == 3:
48+
bfs()
49+
return
50+
51+
# 벽의 개수가 3이 아니면 하나씩 세우기
52+
for i in range(N):
53+
for j in range(M):
54+
if arr[i][j] == 0:
55+
arr[i][j] = 1
56+
makeWall(arr, numOfWall + 1)
57+
# 3개가 돼서 bfs가 끝나면 원복해주기
58+
arr[i][j] = 0
59+
60+
61+
#입력값 받기
62+
inp = sys.stdin.readline
63+
64+
N,M = map(int, inp().split())
65+
66+
arr = []
67+
for _ in range(N):
68+
arr.append(list(map(int, inp().split())))
69+
70+
#바이러스 위치 찾기
71+
virusLocation = []
72+
for i in range(N):
73+
for j in range(M):
74+
if arr[i][j] == 2:
75+
virusLocation.append((i,j))
76+
77+
maxSafeAreaCount = 0
78+
79+
makeWall(arr, 0)
80+
print(maxSafeAreaCount)

0 commit comments

Comments
 (0)