from collections import deque
n = int(input())
matrix = [[int(x) for x in input().split()] for _ in range(n)]
candy = [[-1] * n for _ in range(n)]
offsets = [(1, 0), (0, -1), (-1, 0), (0, 1)]
queue = deque()
for i in range(n):
for j in range(n):
if matrix[i][j] == -3:
candy[i][j] = 0
queue.append((i, j))
ans = -1
while queue:
new_queue = deque()
flag = False
for pos in queue:
x, y = pos
for offset in offsets:
new_x, new_y = x + offset[0], y + offset[1]
if new_x < 0 or new_x >= n or new_y < 0 or new_y >= n or matrix[new_x][new_y] == -1:
continue
if candy[new_x][new_y] == -1:
new_queue.append((new_x, new_y))
candy[new_x][new_y] = max(candy[new_x][new_y], candy[x][y] + max(0, matrix[new_x][new_y]))
if matrix[new_x][new_y] == -2:
ans = candy[new_x][new_y]
flag = True
if flag:
break
queue = new_queue
print(ans)
1.神一样地队友
于 2024-03-26 21:17:15 首次发布