백준 - 18405

developsy·2022년 7월 11일
0
#p344

from collections import deque

n, k = map(int, input().split())
maps = []
viruses = []
biggest = 0
for i in range(n):
    a = list(map(int, input().split()))
    if max(a) > biggest:
        biggest = max(a)
    maps.append(a)
    for j in range(n):
        if maps[i][j] != 0:
            viruses.append((maps[i][j], i, j, 0))
s, x, y = map(int, input().split())
viruses.sort()

second = 0
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
queue = deque(viruses)

while queue:

    current_virus, a, b, second = queue.popleft()
    if second == s:

        break
    
    for i in range(4):
        xx = a + dx[i]
        yy = b + dy[i]
        if 0 <= xx and xx < n and 0 <= yy and yy < n:
            if maps[xx][yy] == 0:
                maps[xx][yy] = current_virus
                queue.append((current_virus, xx, yy, second + 1))
print(maps[x-1][y-1])

구현문제와 bfs를 짬뽕시킨 문제였다. 바이러스의 초기 위치를 어떻게 처음에 저장할까 한참고민했는데, 인풋을 집어넣으면서 for문으로 돌리는 방식이 있었다. 이런 문제가 나오면 다음 번에 응용해봐야겠다.

profile
공부 정리용 블로그

0개의 댓글