[백준 1261] 알고스팟
1. 문제 설명
2. 문제 분석
간선 체크가 아니라 행렬 자체를 입력받아 구현한 다익스트라 알고리즘.
3. 나의 풀이
import sys
import heapq
m, n = map(int, sys.stdin.readline().rstrip().split())
nodes = []
for _ in range(n):
line = list(map(int, input()))
nodes.append(line)
dx = [0, 0, 1, -1]
dy = [1, -1, 0, 0]
INF = sys.maxsize
def Dijkstra():
distances = [[INF for _ in range(m)] for _ in range(n)]
distances[0][0] = 0
pq = []
heapq.heappush(pq, [0, 0, 0])
while pq:
cur_cost, cur_row, cur_col = heapq.heappop(pq)
if distances[cur_row][cur_col] < cur_cost: continue
for x, y in zip(dx, dy):
next_row, next_col = cur_row + y, cur_col + x
if next_row < 0 or next_col < 0 or next_row >= n or next_col >= m: continue
next_cost = nodes[next_row][next_col]
if distances[next_row][next_col] > cur_cost + next_cost:
distances[next_row][next_col] = cur_cost + next_cost
heapq.heappush(pq, [cur_cost + next_cost, next_row, next_col])
print(distances[n-1][m-1])
return
Dijkstra()
Author And Source
이 문제에 관하여([백준 1261] 알고스팟), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://velog.io/@j_aion/백준-1261-알고스팟
저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
간선 체크가 아니라 행렬 자체를 입력받아 구현한 다익스트라 알고리즘.
3. 나의 풀이
import sys
import heapq
m, n = map(int, sys.stdin.readline().rstrip().split())
nodes = []
for _ in range(n):
line = list(map(int, input()))
nodes.append(line)
dx = [0, 0, 1, -1]
dy = [1, -1, 0, 0]
INF = sys.maxsize
def Dijkstra():
distances = [[INF for _ in range(m)] for _ in range(n)]
distances[0][0] = 0
pq = []
heapq.heappush(pq, [0, 0, 0])
while pq:
cur_cost, cur_row, cur_col = heapq.heappop(pq)
if distances[cur_row][cur_col] < cur_cost: continue
for x, y in zip(dx, dy):
next_row, next_col = cur_row + y, cur_col + x
if next_row < 0 or next_col < 0 or next_row >= n or next_col >= m: continue
next_cost = nodes[next_row][next_col]
if distances[next_row][next_col] > cur_cost + next_cost:
distances[next_row][next_col] = cur_cost + next_cost
heapq.heappush(pq, [cur_cost + next_cost, next_row, next_col])
print(distances[n-1][m-1])
return
Dijkstra()
Author And Source
이 문제에 관하여([백준 1261] 알고스팟), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://velog.io/@j_aion/백준-1261-알고스팟
저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
import sys
import heapq
m, n = map(int, sys.stdin.readline().rstrip().split())
nodes = []
for _ in range(n):
line = list(map(int, input()))
nodes.append(line)
dx = [0, 0, 1, -1]
dy = [1, -1, 0, 0]
INF = sys.maxsize
def Dijkstra():
distances = [[INF for _ in range(m)] for _ in range(n)]
distances[0][0] = 0
pq = []
heapq.heappush(pq, [0, 0, 0])
while pq:
cur_cost, cur_row, cur_col = heapq.heappop(pq)
if distances[cur_row][cur_col] < cur_cost: continue
for x, y in zip(dx, dy):
next_row, next_col = cur_row + y, cur_col + x
if next_row < 0 or next_col < 0 or next_row >= n or next_col >= m: continue
next_cost = nodes[next_row][next_col]
if distances[next_row][next_col] > cur_cost + next_cost:
distances[next_row][next_col] = cur_cost + next_cost
heapq.heappush(pq, [cur_cost + next_cost, next_row, next_col])
print(distances[n-1][m-1])
return
Dijkstra()
Author And Source
이 문제에 관하여([백준 1261] 알고스팟), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@j_aion/백준-1261-알고스팟저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)