import sys, heapq
input=sys.stdin.readline
INF = int(1e9)
n = int(input())
maps = []
maps.append([INF]*(n+1))
#배열 입력
for i in range(n):
tmp = [INF]+list(map(int,input().split()))
maps.append(tmp)
#비용 초기화
cost = [[0]*(n+1) for _ in range(n+1)]
cost[1][1]=0
#디피
for i in range(1,n+1):
for j in range(1,n+1):
if j-1==0 and i-1==0:
continue
elif j-1==0:
button2 = cost[i-1][j] if maps[i][j]<maps[i-1][j] else maps[i][j]-maps[i-1][j] + 1 + cost[i-1][j]
cost[i][j]+=button2
elif i-1==0:
button1 = cost[i][j-1] if maps[i][j]<maps[i][j-1] else maps[i][j]-maps[i][j-1] + 1 + cost[i][j-1]
cost[i][j]+=button1
else:
button1 = cost[i][j-1] if maps[i][j]<maps[i][j-1] else maps[i][j]-maps[i][j-1] + 1 + cost[i][j-1]
button2 = cost[i-1][j] if maps[i][j]<maps[i-1][j] else maps[i][j]-maps[i-1][j] + 1 + cost[i-1][j]
cost[i][j]+=min(button1,button2)
#다익스트라 실패
def dijkstra():
q=[]
#담을 리스트 q와 위치까지 드는 최소 비용, 그리고 위치
heapq.heappush(q,(0,(0,0)))
moves = [(1,0),(0,1)]
cost[0][0] = 0
while q:
# 가장 최단 거리가 짧은 노드에 대한 정보 꺼내기
dist, now = heapq.heappop(q)
#현재 노드가 이미 처리된 적이 있는 노드라면 무시
if cost[now[0]][now[1]] < dist:
continue
#현재 노드와 연결된 다른 입접한 노드들을 확인
for move in moves:
row = now[0]+move[0]
col = now[1]+move[1]
#이동할 수 없는 경우
if row ==n or col==n:
continue
button = 0
#제약 조건 확인 후 계산
if maps[row][col]>=maps[now[0]][now[1]]:
button+=maps[row][col]-maps[now[0]][now[1]]+1
hap=dist+button
#지금 노드를 거쳐서 가는게 더 효율적인 경우
if hap < cost[row][col]:
cost[row][col] = hap
heapq.heappush(q,(hap,(row,col)))
print(cost[n][n])
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
3052 KB |
Output is correct |
2 |
Correct |
17 ms |
3052 KB |
Output is correct |
3 |
Correct |
17 ms |
3052 KB |
Output is correct |
4 |
Correct |
17 ms |
3072 KB |
Output is correct |
5 |
Correct |
18 ms |
3052 KB |
Output is correct |
6 |
Correct |
19 ms |
3072 KB |
Output is correct |
7 |
Correct |
18 ms |
3052 KB |
Output is correct |
8 |
Correct |
17 ms |
3052 KB |
Output is correct |
9 |
Correct |
17 ms |
3052 KB |
Output is correct |
10 |
Correct |
17 ms |
3052 KB |
Output is correct |
11 |
Correct |
18 ms |
3052 KB |
Output is correct |
12 |
Correct |
17 ms |
3052 KB |
Output is correct |
13 |
Correct |
17 ms |
3052 KB |
Output is correct |
14 |
Correct |
17 ms |
3052 KB |
Output is correct |
15 |
Correct |
17 ms |
3072 KB |
Output is correct |
16 |
Correct |
17 ms |
3052 KB |
Output is correct |
17 |
Correct |
17 ms |
3180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
3052 KB |
Output is correct |
2 |
Correct |
18 ms |
3052 KB |
Output is correct |
3 |
Correct |
19 ms |
3052 KB |
Output is correct |
4 |
Correct |
17 ms |
3052 KB |
Output is correct |
5 |
Correct |
18 ms |
3052 KB |
Output is correct |
6 |
Correct |
17 ms |
3052 KB |
Output is correct |
7 |
Correct |
19 ms |
3052 KB |
Output is correct |
8 |
Correct |
18 ms |
3052 KB |
Output is correct |
9 |
Correct |
18 ms |
3052 KB |
Output is correct |
10 |
Correct |
23 ms |
3308 KB |
Output is correct |
11 |
Correct |
19 ms |
3072 KB |
Output is correct |
12 |
Correct |
18 ms |
3052 KB |
Output is correct |
13 |
Correct |
18 ms |
3052 KB |
Output is correct |
14 |
Correct |
17 ms |
3052 KB |
Output is correct |
15 |
Correct |
18 ms |
3052 KB |
Output is correct |
16 |
Correct |
17 ms |
3052 KB |
Output is correct |
17 |
Correct |
17 ms |
3052 KB |
Output is correct |
18 |
Correct |
17 ms |
3052 KB |
Output is correct |
19 |
Correct |
18 ms |
3052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
166 ms |
5484 KB |
Output is correct |
2 |
Correct |
140 ms |
5484 KB |
Output is correct |
3 |
Correct |
152 ms |
5484 KB |
Output is correct |
4 |
Correct |
146 ms |
5484 KB |
Output is correct |
5 |
Correct |
139 ms |
5484 KB |
Output is correct |
6 |
Correct |
131 ms |
5504 KB |
Output is correct |
7 |
Correct |
160 ms |
5496 KB |
Output is correct |
8 |
Correct |
131 ms |
5484 KB |
Output is correct |
9 |
Correct |
136 ms |
5496 KB |
Output is correct |
10 |
Correct |
129 ms |
5356 KB |
Output is correct |
11 |
Correct |
135 ms |
5484 KB |
Output is correct |
12 |
Correct |
138 ms |
5504 KB |
Output is correct |
13 |
Correct |
138 ms |
5356 KB |
Output is correct |
14 |
Correct |
114 ms |
5228 KB |
Output is correct |
15 |
Correct |
112 ms |
4736 KB |
Output is correct |
16 |
Correct |
144 ms |
5356 KB |
Output is correct |
17 |
Correct |
154 ms |
5356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1146 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |