Skip to content

Commit 4ef3800

Browse files
committed
[BOJ] #17141. 연구소2 / 골드4 / 1시간 / 실패
1 parent fe5c8ea commit 4ef3800

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import sys
2+
from collections import deque
3+
from itertools import combinations
4+
5+
# 상, 하, 좌, 우
6+
dx = [-1, 1, 0, 0]
7+
dy = [0, 0, -1, 1]
8+
9+
input = sys.stdin.readline
10+
answer = float("inf") # 최솟값을 찾기 위해 초깃값을 inf로 설정
11+
12+
13+
def bfs(v):
14+
queue = deque(v)
15+
visited = [[-1 for _ in range(N)] for _ in range(N)]
16+
min_value = 0 # 최소 횟수를 찾기 위한 변수
17+
for x, y in queue:
18+
visited[x][y] = 0
19+
while queue:
20+
x, y = queue.popleft()
21+
for i in range(4):
22+
ax = x + dx[i]
23+
ay = y + dy[i]
24+
if 0 <= ax < N and 0 <= ay < N:
25+
if visited[ax][ay] == -1 and board[ax][ay] != 1:
26+
queue.append([ax, ay])
27+
visited[ax][ay] = visited[x][y] + 1
28+
min_value = max(min_value, visited[x][y] + 1)
29+
30+
for i in range(N):
31+
for j in range(N):
32+
if visited[i][j] == -1 and board[i][j] != 1:
33+
return float("inf")
34+
return min_value
35+
36+
37+
N, M = map(int, input().split())
38+
board = [list(map(int, input().split())) for _ in range(N)]
39+
virus = []
40+
41+
# 바이러스의 좌표 찾기
42+
for i in range(N):
43+
for j in range(N):
44+
if board[i][j] == 2:
45+
virus.append([i, j])
46+
47+
# 바이러스 좌표들 중 M개를 뽑아 BFS 수행
48+
for v in combinations(virus, M):
49+
answer = min(bfs(v), answer)
50+
51+
# 정답 반환
52+
if answer == float("inf"):
53+
print(-1)
54+
else:
55+
print(answer)

0 commit comments

Comments
 (0)