-
알고리즘: 백준 1261번 알고스팟 (feat.python)알고리즘/백준(BaekJoon) 2021. 1. 11. 18:03
1261번: 알고스팟
첫째 줄에 미로의 크기를 나타내는 가로 크기 M, 세로 크기 N (1 ≤ N, M ≤ 100)이 주어진다. 다음 N개의 줄에는 미로의 상태를 나타내는 숫자 0과 1이 주어진다. 0은 빈 방을 의미하고, 1은 벽을 의미
www.acmicpc.net
import sys import heapq read = sys.stdin.readline m, n = map(int, read().split()) maze = [list(map(int,read().strip()))for i in range(n)] visit = [[0 for _ in range(m)] for _ in range(n)] def dijkstra(): dx = [1, -1, 0, 0] dy = [0, 0, 1, -1] hq = [] heapq.heappush(hq, [0, 0, 0]) while hq: weight, c_x, c_y = heapq.heappop(hq) if visit[c_y][c_x]: continue visit[c_y][c_x] = 1 if c_x == m-1 and c_y == n-1: return weight for i in range(4): if 0<= c_x+dx[i] <m and 0<= c_y+dy[i] <n and not visit[c_y+dy[i]][c_x+dx[i]]: if maze[c_y + dy[i]][c_x + dx[i]] == 1: heapq.heappush(hq, [weight+1, c_x+dx[i], c_y+dy[i] ]) else: heapq.heappush(hq, [weight, c_x + dx[i], c_y + dy[i]]) print(dijkstra())
반응형'알고리즘 > 백준(BaekJoon)' 카테고리의 다른 글
알고리즘: 백준 1504번 특정한 최단 경로 (feat. python) (0) 2021.01.12 알고리즘: 백준 1238번 파티 (feat. python) (0) 2021.01.11 알고리즘: 백준 1753번 최단경로 (feat.python) (0) 2021.01.11 알고리즘: 백준 11724번 연결 요소의 개수 (feat.python) (0) 2021.01.08 알고리즘: 백준 1012번 유기농 배추 (feat. python) (0) 2021.01.07