|
| 1 | +n, m, k = map(int, input().split()) |
| 2 | + |
| 3 | +# 모든 상어의 위치와 방향 정보를 포함하는 2차원 리스트 |
| 4 | +array = [] |
| 5 | +for i in range(n): |
| 6 | + array.append(list(map(int, input().split()))) |
| 7 | + |
| 8 | +# 모든 상어의 현재 방향 정보 |
| 9 | +directions = list(map(int, input().split())) |
| 10 | + |
| 11 | +# 각 위치마다 [특정 냄새의 상어 번호, 특정 냄새의 남은 시간]을 저장하는 2차원 리스트 |
| 12 | +smell = [[[0, 0]] * n for _ in range(n)] |
| 13 | + |
| 14 | +# 각 상어의 회전 우선순위 정보 |
| 15 | +priorities = [[] for _ in range(m)] |
| 16 | +for i in range(m): |
| 17 | + for j in range(4): |
| 18 | + priorities[i].append(list(map(int, input().split()))) |
| 19 | + |
| 20 | +# 특정 위치에서 이동 가능한 4가지 방향 |
| 21 | +dx = [-1, 1, 0, 0] |
| 22 | +dy = [0, 0, -1, 1] |
| 23 | + |
| 24 | +# 모든 냄새 정보를 업데이트 |
| 25 | +def update_smell(): |
| 26 | + # 각 위치를 하나씩 확인하며 |
| 27 | + for i in range(n): |
| 28 | + for j in range(n): |
| 29 | + # 냄새가 존재하는 경우, 시간을 1만큼 감소시키기 |
| 30 | + if smell[i][j][1] > 0: |
| 31 | + smell[i][j][1] -= 1 |
| 32 | + # 상어가 존재하는 해당 위치의 냄새를 k로 설정 |
| 33 | + if array[i][j] != 0: |
| 34 | + smell[i][j] = [array[i][j], k] |
| 35 | + |
| 36 | +# 모든 상어를 이동시키는 함수 |
| 37 | +def move(): |
| 38 | + # 이동 결과를 담기 위한 임시 결과 테이블 초기화 |
| 39 | + new_array = [[0] * n for _ in range(n)] |
| 40 | + # 각 위치를 하나씩 확인하며 |
| 41 | + for x in range(n): |
| 42 | + for y in range(n): |
| 43 | + # 상어가 존재하는 경우 |
| 44 | + if array[x][y] != 0: |
| 45 | + direction = directions[array[x][y] - 1] # 현재 상어의 방향 |
| 46 | + found = False |
| 47 | + # 일단 냄새가 존재하지 않는 곳이 있는지 확인 |
| 48 | + for index in range(4): |
| 49 | + nx = x + dx[priorities[array[x][y] - 1][direction - 1][index] - 1] |
| 50 | + ny = y + dy[priorities[array[x][y] - 1][direction - 1][index] - 1] |
| 51 | + if 0 <= nx and nx < n and 0 <= ny and ny < n: |
| 52 | + if smell[nx][ny][1] == 0: # 냄새가 존재하지 않는 곳이면 |
| 53 | + # 해당 상어의 방향 이동시키기 |
| 54 | + directions[array[x][y] - 1] = priorities[array[x][y] - 1][direction - 1][index] |
| 55 | + # 상어 이동시키기 (만약 이미 다른 상어가 있다면 번호가 낮은 것이 들어가도록) |
| 56 | + if new_array[nx][ny] == 0: |
| 57 | + new_array[nx][ny] = array[x][y] |
| 58 | + else: |
| 59 | + new_array[nx][ny] = min(new_array[nx][ny], array[x][y]) |
| 60 | + found = True |
| 61 | + break |
| 62 | + if found: |
| 63 | + continue |
| 64 | + # 주변에 모두 냄새가 남아 있다면, 자신의 냄새가 있는 곳으로 이동 |
| 65 | + for index in range(4): |
| 66 | + nx = x + dx[priorities[array[x][y] - 1][direction - 1][index] - 1] |
| 67 | + ny = y + dy[priorities[array[x][y] - 1][direction - 1][index] - 1] |
| 68 | + if 0 <= nx and nx < n and 0 <= ny and ny < n: |
| 69 | + if smell[nx][ny][0] == array[x][y]: # 자신의 냄새가 있는 곳이면 |
| 70 | + # 해당 상어의 방향 이동시키기 |
| 71 | + directions[array[x][y] - 1] = priorities[array[x][y] - 1][direction - 1][index] |
| 72 | + # 상어 이동시키기 |
| 73 | + new_array[nx][ny] = array[x][y] |
| 74 | + break |
| 75 | + return new_array |
| 76 | + |
| 77 | +time = 0 |
| 78 | +while True: |
| 79 | + update_smell() # 모든 위치의 냄새를 업데이트 |
| 80 | + new_array = move() # 모든 상어를 이동시키기 |
| 81 | + array = new_array # 맵 업데이트 |
| 82 | + time += 1 # 시간 증가 |
| 83 | + |
| 84 | + # 1번 상어만 남았는지 체크 |
| 85 | + check = True |
| 86 | + for i in range(n): |
| 87 | + for j in range(n): |
| 88 | + if array[i][j] > 1: |
| 89 | + check = False |
| 90 | + if check: |
| 91 | + print(time) |
| 92 | + break |
| 93 | + |
| 94 | + # 1000초가 지날 때까지 끝나지 않았다면 |
| 95 | + if time >= 1000: |
| 96 | + print(-1) |
| 97 | + break |
0 commit comments