728x90
문제링크
https://www.acmicpc.net/problem/18405
코드
# 경쟁적 전염 - bfs
from collections import deque
N, K = map(int, input().split())
graph = []
virus = []
for i in range(N):
graph.append(list(map(int, input().split())))
for j in range(N):
if graph[i][j] != 0:
virus.append(((graph[i][j], i, j)))
S, X, Y = map(int, input().split())
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
def bfs(s, X, Y):
que = deque(virus)
count = 0
while que:
if count == s:
break
for _ in range(len(que)):
prev, x, y = que.popleft()
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0 <= nx < N and 0 <= ny < N:
if graph[nx][ny] == 0:
graph[nx][ny] = graph[x][y]
que.append((graph[nx][ny], nx, ny))
count += 1
return graph[X-1][Y-1]
virus.sort()
print(bfs(S, X, Y))
728x90
'코딩테스트' 카테고리의 다른 글
BFS&DFS_심화19) 연산자 끼워 넣기 _ python (0) | 2023.09.25 |
---|---|
BFS&DFS_심화18) 괄호변환 _ python (0) | 2023.09.25 |
BFS&DFS_심화16) 연구소 _ python (0) | 2023.09.25 |
BFS&DFS_심화15) 특정 거리의 도시 찾기 _ python (0) | 2023.09.25 |
백트래킹(Backtracking) (1) | 2023.09.25 |