|
2 | 2 |
|
3 | 3 | n, k = map(int, input().split())
|
4 | 4 |
|
5 |
| -board = [] # 전체 보드 정보를 담는 리스트 |
| 5 | +graph = [] # 전체 보드 정보를 담는 리스트 |
6 | 6 | data = [] # 바이러스에 대한 정보를 담는 리스트
|
7 | 7 |
|
8 | 8 | for i in range(n):
|
9 | 9 | # 보드 정보를 한 줄 단위로 입력
|
10 |
| - board.append(list(map(int, input().split()))) |
| 10 | + graph.append(list(map(int, input().split()))) |
11 | 11 | for j in range(n):
|
12 | 12 | # 해당 위치에 바이러스가 존재하는 경우
|
13 |
| - if board[i][j] != 0: |
| 13 | + if graph[i][j] != 0: |
14 | 14 | # (바이러스 종류, 시간, 위치 X, 위치 Y) 삽입
|
15 |
| - data.append((board[i][j], 0, i, j)) |
| 15 | + data.append((graph[i][j], 0, i, j)) |
16 | 16 |
|
17 |
| -# 정렬 이후에 큐로 옮기기 |
| 17 | +# 정렬 이후에 큐로 옮기기 (낮은 번호의 바이러스가 먼저 증식하므로) |
18 | 18 | data.sort()
|
19 | 19 | q = deque(data)
|
20 | 20 |
|
|
23 | 23 | # 바이러스가 퍼져나갈 수 있는 4가지의 위치
|
24 | 24 | dx = [-1, 0, 1, 0]
|
25 | 25 | dy = [0, 1, 0, -1]
|
26 |
| - |
| 26 | + |
| 27 | +# 너비 우선 탐색(BFS) 진행 |
27 | 28 | while q:
|
28 | 29 | virus, s, x, y = q.popleft()
|
29 | 30 | # 정확히 s초가 지나거나, 큐가 빌 때까지 반복
|
30 | 31 | if s == target_s:
|
31 | 32 | break
|
32 |
| - # 4가지 위치를 각각 확인 |
| 33 | + # 현재 노드에서 주변 4가지 위치를 각각 확인 |
33 | 34 | for i in range(4):
|
34 | 35 | nx = x + dx[i]
|
35 | 36 | ny = y + dy[i]
|
36 | 37 | # 해당 위치로 이동할 수 있는 경우
|
37 | 38 | if 0 <= nx and nx < n and 0 <= ny and ny < n:
|
38 | 39 | # 아직 방문하지 않은 위치라면, 그 위치에 바이러스 넣기
|
39 |
| - if board[nx][ny] == 0: |
40 |
| - board[nx][ny] = virus |
| 40 | + if graph[nx][ny] == 0: |
| 41 | + graph[nx][ny] = virus |
41 | 42 | q.append((virus, s + 1, nx, ny))
|
42 | 43 |
|
43 |
| -print(board[target_x - 1][target_y - 1]) |
| 44 | +print(graph[target_x - 1][target_y - 1]) |
0 commit comments