백준 2178 미로탐색

김민영·2022년 12월 28일
0

알고리즘

목록 보기
6/125

미로탐색

계획

  • BFS. 큐 사용
import sys
from collections import deque

N, M = map(int, input().split())
map_lst = []
count_lst = []
for _ in range(N):
    map_lst.append(list(map(int, input())))
    count_lst.append([0] * M)

# 상 우 하 좌
dx = [0, 1, 0, -1]
dy = [1, 0, -1, 0]

def bfs(x, y):
    queue = deque([[x, y]])
    map_lst[y][x] = 0
    while queue:
        index = queue.popleft()
        x = index[0]
        y = index[1]
        for i in range(4):
            nx = x + dx[i]
            ny = y + dy[i]
            if nx < 0 or ny < 0 or nx >= M or ny >= N:
                continue
            if map_lst[ny][nx] != 0:
                map_lst[ny][nx] = 0
                count_lst[ny][nx] = count_lst[y][x] + 1
                queue.append([nx, ny])

bfs(0, 0)
print(count_lst[N-1][M-1]+1)
profile
노션에 1차 정리합니당 - https://cream-efraasia-f3c.notion.site/4fb02c0dc82e48358e67c61b7ce8ab36?v=

0개의 댓글