티스토리 뷰
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 = ((0, 1), (1, 0), (0, -1), (-1, 0))
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로 제한되니 완탐으로 충분히 풀 수 있다.
파이썬이라서 시간초과 날까봐 조마조마 했는데, 통과되더라
'algorithm'''problem solve' 카테고리의 다른 글
[프로그래머스] 괄호 변환 (DFS, 구현, 카카오) (0) | 2020.10.04 |
---|---|
[백준] 18405 - 경쟁적 전염 (BFS) (0) | 2020.10.04 |
[백준] 18352 - 특정 거리의 도시 찾기 (BFS) (0) | 2020.10.03 |
[프로그래머스] 외벽 점검 (카카오, 구현, 순열) (0) | 2020.10.02 |
[백준] 15686 - 치킨 배달 (구현, 브루트포스) (0) | 2020.10.02 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 카카오
- 이분탐색
- 정렬
- PyPy3
- 동적 계획법
- webOS
- 브루트포스
- 구현
- Dynamic Programming
- DP
- BaekJoon
- 오픈소스
- 프로그래머스
- 한화큐셀
- 컨트리뷰톤
- 완전탐색
- c
- DFS
- 피보나치
- 인공지능
- LG
- c++
- 백준
- 백트래킹
- 파이썬
- 1932
- 플로이드 와셜
- 코딩
- 알고리즘
- BFS
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함