https://www.acmicpc.net/problem/17141
17141번: 연구소 2
인체에 치명적인 바이러스를 연구하던 연구소에 승원이가 침입했고, 바이러스를 유출하려고 한다. 승원이는 연구소의 특정 위치에 바이러스 M개를 놓을 것이고, 승원이의 신호와 동시에 바이
www.acmicpc.net
코드
import itertools
import sys
import queue
n, m = map(int, sys.stdin.readline().split())
lis = []
two_cnt = 0
two_point = []
for i in range(n): lis.append(list(map(int, sys.stdin.readline().split())))
for i in range(n):
for j in range(n):
if lis[i][j] == 2:
two_cnt += 1
two_point.append([i, j])
tmp = [i for i in range(two_cnt)]
d = [[1, 0], [0, 1], [-1, 0], [0, -1]]
q = queue.Queue()
ans = sys.maxsize
for combination in itertools.combinations(tmp, m):
check = True
value = [[0 for i in range(n)] for j in range(n)]
for combi in combination:
value[two_point[combi][0]][two_point[combi][1]] = 1
q.put(two_point[combi] + [1])
while q.empty() == False:
v = q.get()
for i in d:
if v[0] + i[0] < n and v[0] + i[0] >= 0 and v[1] + i[1] < n and v[1] + i[1] >= 0:
if lis[v[0] + i[0]][v[1] + i[1]] != 1 and value[v[0] + i[0]][v[1] + i[1]] == 0:
value[v[0] + i[0]][v[1] + i[1]] = v[2] + 1
q.put([v[0] + i[0], v[1] + i[1], v[2] + 1])
t = 0
for i in range(n):
for j in range(n):
if lis[i][j] == 1: value[i][j] = -1
if value[i][j] == 0: check = False
if check: ans = min(ans, max(map(max, value)))
print(ans - 1 if ans != sys.maxsize else -1)
|
cs |
풀이
1. 일반적인 2차원 평면 위의 BFS를 구현합니다
2. 바이러스 위치를 저장 후 itertools.combinations로 가능 한 모든 바이러스의 위치를 표시 합니다.
'백준(Python) > 골드' 카테고리의 다른 글
백준 13460: 구슬 탈출 2 [Python], 삼성 코딩 테스트 (0) | 2022.06.13 |
---|---|
백준 15927: 회문은 회문아니야!! [파이썬] (0) | 2022.06.03 |
[Python] 백준 1520번 - 내리막 길, 다이나믹 프로그래밍-기초 (0) | 2022.05.26 |