Skip to content

Commit 5f53bb9

Browse files
committed
[BOJ]14502_연구소/ Gold / 60min
https://www.acmicpc.net/problem/14502
1 parent f74f4e7 commit 5f53bb9

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

Hongjoo/백준/연구소.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
2+
3+
"""
4+
5+
(1) 벽 3개 - 세우기 -> 0중 3 개 조합 구하기
6+
(2) BFS -> 확산시키기
7+
(3) 안전 영역 계산하기 -> min 이란 비교시 업데이트
8+
(4) 조합 replay
9+
10+
#bfs
11+
visited = [False] * 9
12+
def bfs(graph, start, visited):
13+
# start -> queue, visited
14+
queue = deque([start])
15+
visited[start] = True
16+
while queue :
17+
node= queue.popleft()
18+
for i in graph[node]:
19+
if not visited[i]:
20+
queue.append(i)
21+
visited[i] = True
22+
23+
24+
"""
25+
from itertools import combinations
26+
from collections import deque
27+
import sys
28+
import copy
29+
30+
a,b= map(int, sys.stdin.readline().split()) # row, columns
31+
32+
m = []
33+
zero_p = []
34+
virus_p = []
35+
for i in range(a):
36+
tmp = list(map(int, input().split()))
37+
for j in range(b) :
38+
if tmp[j] == 0:
39+
zero_p.append([i,j])
40+
elif tmp[j] == 2 :
41+
virus_p.append([i,j])
42+
m.append(tmp)
43+
# print(m)
44+
#1. zero_p 중 3개 조합 구하기 -> combi
45+
combi = list(combinations(zero_p , 3))
46+
47+
#2. BFS
48+
max_safe= 0
49+
for walls in combi:
50+
graph=copy.deepcopy(m)
51+
# 벽설치
52+
for w_x, w_y in walls :
53+
graph[w_x][w_y] = 1
54+
55+
new_v_count =0
56+
# virus 들위 위치 = BFS start 위치
57+
for v in range(len(virus_p)) :
58+
start=virus_p[v] # start = [x,y]
59+
queue = deque([start])
60+
#bfs
61+
while queue :
62+
node = queue.popleft()
63+
# print("#",node, end ="")
64+
# 인근 node - 상/하/좌/우
65+
near_node = [[node[0]+1,node[1]] ,
66+
[node[0]-1,node[1]],
67+
[node[0],node[1]+1],
68+
[node[0],node[1]-1] ]
69+
70+
for nn in near_node :
71+
x,y = nn
72+
# out of range
73+
if x < 0 or x >= a or y <0 or y>=b :
74+
# print("bump!")
75+
continue
76+
# print(x,y)
77+
if graph[x][y] == 0 : #방문 x
78+
queue.append(nn)
79+
graph[x][y] = 2 # 방문 완료
80+
new_v_count+= 1
81+
82+
83+
#(3) 안전 영역 계산하기 -> min 이란 비교시 업데이트
84+
safe_area = len(zero_p)-3 -new_v_count
85+
if max_safe < safe_area :
86+
max_safe = safe_area
87+
print(f"{max_safe}")
88+
89+

0 commit comments

Comments
 (0)