티스토리 뷰

https://www.acmicpc.net/problem/14502

 

14502번: 연구소

인체에 치명적인 바이러스를 연구하던 연구소에서 바이러스가 유출되었다. 다행히 바이러스는 아직 퍼지지 않았고, 바이러스의 확산을 막기 위해서 연구소에 벽을 세우려고 한다. 연구소는 크�

www.acmicpc.net

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
from itertools import combinations
from copy import deepcopy
 
dd = ((01), (10), (0-1), (-10))
n, m = map(int, input().split())
graph = list()
empty = list()
virus = list()
for i in range(n):
    tmp = list(map(int, input().split()))
    for j in range(m):
        if tmp[j] == 2:
            virus.append([i, j])
        elif tmp[j] == 0:
            empty.append([i, j])
    graph.append(tmp)
 
 
def dfs(graph, y, x):
    graph[y][x] = 2
    for i in range(4):
        posy = y + dd[i][0]
        posx = x + dd[i][1]
        if 0 <= posy < n and 0 <= posx < m and graph[posy][posx] == 0:
            dfs(graph, posy, posx)
 
 
answer = 0
for comb in combinations(empty, 3):
    tmp_graph = deepcopy(graph)
    for y, x in comb:
        tmp_graph[y][x] = 1
    count = 0
    for i in range(n):
        for j in range(m):
            if tmp_graph[i][j] == 2:
                dfs(tmp_graph, i, j)
    for g in tmp_graph:
        count += g.count(0)
    if answer < count:
        answer = count
print(answer)
cs

완전탐색과 DFS를 조합하여 풀 수 있다.

높이와 너비가 7, 8로 제한되니 완탐으로 충분히 풀 수 있다.

파이썬이라서 시간초과 날까봐 조마조마 했는데, 통과되더라

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
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
글 보관함