Skip to content

Commit 2723e4d

Browse files
committed
[BOJ] #18405. 경쟁적 전염 / 골드5 / 48분 / 실패
1 parent 6f50dae commit 2723e4d

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

.DS_Store

0 Bytes
Binary file not shown.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import sys
2+
from collections import deque
3+
input = sys.stdin.readline
4+
5+
def get_result(goal_sec, goal_x, goal_y, grid, n, k):
6+
virus_info = [] # 바이러스 정보를 담는 리스트 초기화
7+
8+
# 시험관을 순회하며 바이러스 팀섹
9+
for i in range(n):
10+
for j in range(n):
11+
if grid[i][j] != 0: # 바이러스라면
12+
virus_info.append((grid[i][j], 0, i, j)) # 바이러스 번호, 현재 시간, x좌표, y좌표를 추가
13+
14+
virus_info.sort() # 바이러스 정보 리스트를 오름차순으로 정렬
15+
queue = deque(virus_info) # 큐 초기화
16+
directions = [(-1, 0), (1, 0), (0, -1), (0, 1)] # 방향 리스트 (상, 하, 좌, 우)
17+
18+
while queue:
19+
virus_num, time, x, y = queue.popleft()
20+
21+
# 목표 시간에 도달하면 종료
22+
if time == goal_sec:
23+
break
24+
25+
for dx, dy in directions:
26+
nx, ny = x + dx, y + dy # 현재 지점에서 상하좌우 인접한 지점
27+
# 시험관 범위 안에 있고 아직 바이러스가 없는 경우 현재의 바이러스를 전파
28+
if (0 <= nx < n and 0 <= ny < n) and grid[nx][ny] == 0:
29+
grid[nx][ny] = virus_num
30+
queue.append((virus_num, time + 1, nx, ny))
31+
32+
return grid[goal_x][goal_y]
33+
34+
# 입력 처리
35+
n, k = map(int, input().split()) # n: 시험관 크기 / k: 바이러스 개수
36+
grid = [] # 시험관 정보
37+
for _ in range(n):
38+
li = list(map(int, input().split()))
39+
grid.append(li)
40+
s, x, y = map(int, input().split()) # 시간, x좌표, y좌표
41+
42+
# 함수 호출 및 결과 출력
43+
print(get_result(s, x-1, y-1, grid, n, k))

0 commit comments

Comments
 (0)