Life Engineering
[BOJ 14502] 연구소 (Python3) 본문
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
|
from itertools import combinations
from collections import deque
import copy
N, M=map(int, input().split())
graph=[]
l=[]
candidates=[]
virus=[]
dx=[-1,1,0,0]
dy=[0,0,-1,1]
result=0
for i in range(N):
graph.append(list(map(int, input().split())))
for j in range(M):
if graph[i][j]==0:
l.append((i,j))
elif graph[i][j]==2:
virus.append((i,j))
candidates=list(combinations(l,3))
length=len(candidates)
def bfs(walls):
count=0
g=copy.deepcopy(graph)
for x,y in walls:
g[x][y]=1
queue=deque(virus)
while queue:
x,y=queue.popleft()
for i in range(4):
nx=x+dx[i]
ny=y+dy[i]
if nx<0 or ny<0 or nx>=N or ny>=M:
continue
if g[nx][ny]==0:
g[nx][ny]=2
queue.append((nx,ny))
for i in range(N):
for j in range(M):
if g[i][j]==0:
count+=1
return count
for i in range(length):
result=max(result,bfs(candidates[i]))
print(result)
|
cs |
BFS 로 풀었다.
벽을 3개를 세워야 하니까 가능한 경우의 수를 combination 하여 구한다.
각 경우마다 BFS 탐색을 하여 최대값을 찾는다.
다 최대로 하여 시간복잡도 계산할 경우 260만?이 나와서 이렇게 하기로 했다.
다 풀고 나니 브루트 포스+BFS 라고 카테고리 설정이 되어 있었다.
'Problem Solving' 카테고리의 다른 글
[BOJ 11060] 점프 점프 (Python3) (0) | 2021.02.15 |
---|---|
[BOJ 15686] 치킨 배달 (Python3) (0) | 2021.02.15 |
[BOJ 1182] 부분수열의 합 (Python3) (0) | 2021.02.14 |
[BOJ 14889] 스타트와 링크(Python3) (0) | 2021.02.14 |
[BOJ 9205] 맥주 마시면서 걸어가기 (Python3) (0) | 2021.02.14 |