|
| 1 | +from collections import deque |
| 2 | + |
| 3 | + |
| 4 | +def solve(): |
| 5 | + N, K, L = map(int, input().split()) |
| 6 | + dt = [] |
| 7 | + robots = deque() |
| 8 | + |
| 9 | + for _ in range(N): |
| 10 | + dt.append(list(map(int, input().split()))) |
| 11 | + |
| 12 | + for _ in range(K): |
| 13 | + r, c = list(map(int, input().split())) |
| 14 | + robots.append([r - 1, c - 1]) |
| 15 | + |
| 16 | + for _ in range(L): |
| 17 | + robots_next = deque() |
| 18 | + |
| 19 | + # 1 청소기 이동 |
| 20 | + while robots: |
| 21 | + robot_sr, robot_sc = robots.popleft() |
| 22 | + visited = [[False] * N for _ in range(N)] |
| 23 | + |
| 24 | + # 로봇이 있는 위치는 방문할 수 없도록, True로 표시함 |
| 25 | + for r, c in robots + robots_next: |
| 26 | + visited[r][c] = True |
| 27 | + |
| 28 | + candidates = [] |
| 29 | + if dt[robot_sr][robot_sc] > 0: |
| 30 | + candidates.append([0, robot_sr, robot_sc]) |
| 31 | + |
| 32 | + queue = deque([[0, robot_sr, robot_sc]]) |
| 33 | + visited[robot_sr][robot_sc] = True |
| 34 | + |
| 35 | + while queue: |
| 36 | + depth, sr, sc = queue.popleft() |
| 37 | + drc = [[0, 1], [1, 0], [0, -1], [-1, 0]] |
| 38 | + |
| 39 | + for dr, dc in drc: |
| 40 | + nr, nc = sr + dr, sc + dc |
| 41 | + if 0 <= nr < N and 0 <= nc < N and visited[nr][nc] == False: |
| 42 | + if dt[nr][nc] == 0: # 빈칸이면 |
| 43 | + queue.append([depth +1, nr, nc]) |
| 44 | + elif dt[nr][nc] > 0: # 먼지가 있으면 |
| 45 | + candidates.append([depth +1, nr, nc]) |
| 46 | + |
| 47 | + visited[nr][nc] = True |
| 48 | + |
| 49 | + if len(candidates) == 0: |
| 50 | + robots_next.append([robot_sr, robot_sc]) |
| 51 | + else: |
| 52 | + candidates.sort() |
| 53 | + _, r, c = candidates[0] |
| 54 | + robots_next.append([r, c]) |
| 55 | + |
| 56 | + robots = robots_next |
| 57 | + |
| 58 | + # 2 청소 |
| 59 | + for r_r, r_c in robots: |
| 60 | + banghyang = [ |
| 61 | + [[0, 1], [1, 0], [-1, 0], [0, 0]], # 오른쪽 |
| 62 | + [[0, 1], [1, 0], [0, -1], [0, 0]], # 아래쪽 |
| 63 | + [[1, 0], [0, -1], [-1, 0], [0, 0]], # 왼쪽 |
| 64 | + [[0, 1], [0, -1], [-1, 0], [0, 0]] # 위쪽 |
| 65 | + ] |
| 66 | + |
| 67 | + # 어느 방향을 청소해야 하는지 찾기 |
| 68 | + candidates = [] |
| 69 | + for idx_banghyang in range(len(banghyang)): |
| 70 | + local_candi = 0 |
| 71 | + for bh_r, bh_c in banghyang[idx_banghyang]: |
| 72 | + nr, nc = bh_r + r_r, bh_c + r_c |
| 73 | + if 0 <= nr < N and 0 <= nc < N: |
| 74 | + if dt[nr][nc] > 0: # 이동할수 있는 칸이고, 먼지가 있는 칸일 때 |
| 75 | + local_candi += min(20, dt[nr][nc]) |
| 76 | + |
| 77 | + candidates.append([local_candi * -1, idx_banghyang]) |
| 78 | + |
| 79 | + candidates.sort() |
| 80 | + # idx_banghyang 방향이 먼지량이 가장큰 방향 |
| 81 | + _, idx_banghyang = candidates[0] |
| 82 | + |
| 83 | + for bh_r, bh_c in banghyang[idx_banghyang]: |
| 84 | + nr, nc = bh_r + r_r, bh_c + r_c |
| 85 | + if 0 <= nr < N and 0 <= nc < N: |
| 86 | + if dt[nr][nc] > 0: # 먼지가 있는 칸일 때 |
| 87 | + dt[nr][nc] -= 20 # 격자마다 청소할 수 있는 최대 먼지량은 20입니닫. |
| 88 | + if dt[nr][nc] < 0: |
| 89 | + dt[nr][nc] = 0 |
| 90 | + |
| 91 | + # 3. 먼지 축적 |
| 92 | + for r in range(len(dt)): |
| 93 | + for c in range(len(dt[0])): |
| 94 | + if dt[r][c] > 0: |
| 95 | + dt[r][c] += 5 |
| 96 | + |
| 97 | + # 4. 먼지 확산 |
| 98 | + tmp = [[0] * N for _ in range(N)] |
| 99 | + for r in range(len(dt)): |
| 100 | + for c in range(len(dt[0])): |
| 101 | + if dt[r][c] == 0: |
| 102 | + drc = [[0, 1], [1, 0], [0, -1], [-1, 0]] |
| 103 | + dust_0 = 0 |
| 104 | + for dr, dc in drc: |
| 105 | + nr, nc = r + dr, c + dc |
| 106 | + if 0 <= nr < N and 0 <= nc < N: |
| 107 | + if dt[nr][nc] > 0: |
| 108 | + dust_0 += dt[nr][nc] |
| 109 | + tmp[r][c] = (dust_0 // 10) |
| 110 | + |
| 111 | + for r in range(len(dt)): |
| 112 | + for c in range(len(dt[0])): |
| 113 | + if tmp[r][c] > 0: |
| 114 | + dt[r][c] += tmp[r][c] |
| 115 | + |
| 116 | + answer = 0 |
| 117 | + for r in range(len(dt)): |
| 118 | + for c in range(len(dt[0])): |
| 119 | + if dt[r][c] > 0: |
| 120 | + answer += dt[r][c] |
| 121 | + |
| 122 | + print(answer) |
| 123 | + |
| 124 | + if answer == 0: |
| 125 | + return |
| 126 | + |
| 127 | + |
| 128 | +# Press the green button in the gutter to run the script. |
| 129 | +if __name__ == '__main__': |
| 130 | + solve() |
0 commit comments