-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #169 from AlgoLeadMe/41-pknujsp
41-pknujsp
- Loading branch information
Showing
2 changed files
with
35 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from heapq import * | ||
from sys import * | ||
|
||
C, R = map(int, stdin.readline().split()) | ||
arr = [list(map(int, list(stdin.readline().strip()))) for _ in range(R)] | ||
|
||
drc = ((1,0),(-1,0),(0,1),(0,-1)) | ||
visited = [[False] * C for _ in range(R)] | ||
heap = [(0, 0, 0)] | ||
target_r = R - 1 | ||
target_c = C - 1 | ||
|
||
while heap: | ||
cost, r, c = heappop(heap) | ||
|
||
if r == target_r and c == target_c: | ||
print(cost) | ||
break | ||
if visited[r][c]: | ||
continue | ||
|
||
visited[r][c] = True | ||
|
||
for dr, dc in drc: | ||
nr = r + dr | ||
nc = c + dc | ||
|
||
if not 0 <= nr < R or not 0 <= nc < C: | ||
continue | ||
if visited[nr][nc]: | ||
continue | ||
|
||
heappush(heap, (cost + arr[nr][nc], nr, nc)) |