문제
어른 상어
코드
n, m, k = map(int, input().split())
dxy = [[], [-1, 0], [1, 0], [0, -1], [0, 1]]
shark = {}
for i in range(n):
grid = list(map(int, input().split()))
for j in range(n):
if grid[j] > 0:
shark[grid[j]] = [i, j]
direc = list(map(int, input().split()))
for i, d in enumerate(direc):
shark[i+1].append(d)
direc_priority = {}
for i in range(1, m+1):
direc_priority[i] = [[]]
for _ in range(4):
direc_priority[i].append(list(map(int, input().split())))
board = [[[0, 0] for _ in range(n)] for _ in range(n)]
def spray_smell():
for i in range(1, m+1):
if i not in shark: continue
x, y, dir = shark[i]
board[x][y] = [k, i]
def move():
loc = {}
for i in range(1, m+1):
if i not in shark: continue
x, y, dir = shark[i]
mysmell = []
moved = False
for d in direc_priority[i][dir]:
nx, ny = x + dxy[d][0], y + dxy[d][1]
if nx < 0 or nx >= n or ny < 0 or ny >= n: continue
if board[nx][ny][0] > 0 and board[nx][ny][1] != i: continue
if board[nx][ny][0] > 0:
mysmell.append(d)
continue
shark[i] = [nx, ny, d]
if (nx, ny) in loc:
del shark[max(i, loc[(nx, ny)])]
loc[(nx, ny)] = min(loc[(nx, ny)], i)
else:
loc[(nx, ny)] = i
if loc[(nx, ny)] == i:
moved = True
break
if not moved and i in shark:
d = mysmell[0]
nx, ny = x + dxy[d][0], y + dxy[d][1]
loc[(nx, ny)] = i
shark[i] = [nx, ny, d]
for i in range(n):
for j in range(n):
if 0 < board[i][j][0] <= k:
board[i][j][0] -= 1
sec = 0
while len(shark) > 1 and sec <= 1000:
spray_smell()
move()
sec += 1
if sec > 1000: print(-1)
else: print(sec)