728x90

Queue + BFS 사용
https://www.acmicpc.net/problem/18405
18405번: 경쟁적 전염
첫째 줄에 자연수 N, K가 공백을 기준으로 구분되어 주어진다. (1 ≤ N ≤ 200, 1 ≤ K ≤ 1,000) 둘째 줄부터 N개의 줄에 걸쳐서 시험관의 정보가 주어진다. 각 행은 N개의 원소로 구성되며, 해당 위치
www.acmicpc.net
from collections import deque
n, m = map(int, input().split())
graph = []
data = []
for i in range(n):
graph.append(list(map(int, input().split())))
for j in range(n):
if graph[i][j] != 0:
data.append([graph[i][j], 0, i, j])
data.sort()
print(data)
q = deque(data)
target_time, target_x, target_y = map(int, input().split())
dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]
while q:
virus, time, x, y = q.popleft()
if time == target_time:
break
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0<=nx and nx<n and 0<=ny and ny<n:
if graph[nx][ny] == 0:
graph[nx][ny] = virus
q.append([virus, time+1, nx, ny])
print(graph[target_x-1][target_y-1])
728x90
'Algorithm > Baekjoon' 카테고리의 다른 글
(Brute Force)백준 2798 블랙잭 - python (0) | 2021.05.16 |
---|---|
(Stack)백준 2504 괄호의 값 - python (0) | 2021.03.31 |
(Floyd)백준 11404 플로이드 - python (0) | 2021.03.15 |
(DFS)백준 14502 연구소 - python (0) | 2021.03.13 |
(BFS)백준 18352 특정 거리의 도시 찾기 - python (0) | 2021.03.12 |