From 1d861764b87e00fb879c8cf9e804090148dd4f72 Mon Sep 17 00:00:00 2001 From: Yoonseo Kim Date: Sun, 13 Feb 2022 18:18:23 +0900 Subject: [PATCH] =?UTF-8?q?=EB=AF=B8=EB=A1=9C=ED=83=88=EC=B6=9C=20(#4)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Chapter 5/5-11.py" | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 "Algorithm/\354\235\264\352\262\203\354\235\264 \354\267\250\354\227\205\354\235\204 \354\234\204\355\225\234 \354\275\224\353\224\251 \355\205\214\354\212\244\355\212\270\353\213\244/Chapter 5/5-11.py" diff --git "a/Algorithm/\354\235\264\352\262\203\354\235\264 \354\267\250\354\227\205\354\235\204 \354\234\204\355\225\234 \354\275\224\353\224\251 \355\205\214\354\212\244\355\212\270\353\213\244/Chapter 5/5-11.py" "b/Algorithm/\354\235\264\352\262\203\354\235\264 \354\267\250\354\227\205\354\235\204 \354\234\204\355\225\234 \354\275\224\353\224\251 \355\205\214\354\212\244\355\212\270\353\213\244/Chapter 5/5-11.py" new file mode 100644 index 0000000..8aa8b47 --- /dev/null +++ "b/Algorithm/\354\235\264\352\262\203\354\235\264 \354\267\250\354\227\205\354\235\204 \354\234\204\355\225\234 \354\275\224\353\224\251 \355\205\214\354\212\244\355\212\270\353\213\244/Chapter 5/5-11.py" @@ -0,0 +1,37 @@ +# N x M 크기의 직사각형 형태의 미로 +# 동빈이의 위치는 (1,1) 미로의 출구는 (N, M) +# 괴물이 있는 부분은 0, 괴물이 없는 부분은 1 +# 이때 동빈이가 탈출하기 위해 움직여야하는 최소 칸의 개수를 구하는 프로그램 + +from collections import deque + +# 입력 N과 M +n, m = map(int, input().split()) + +# 2차원 리스트 입력 +graph = [] +for i in range(n): + graph.append(list(map(int, input()))) + + +dx = [-1, 1, 0, 0] +dy = [0, 0, -1, 1] + +queue = deque() +queue.append((0, 0)) + +while queue: + x, y = queue.popleft() + + if x == n-1 and y == m-1: + print(graph[n-1][m-1]) + break + + for i in range(4): + nx = x + dx[i] + ny = y + dy[i] + + if 0 <= nx < n and 0 <= ny < m: + if graph[nx][ny] == 1: + graph[nx][ny] = graph[x][y] + 1 + queue.append((nx, ny)) \ No newline at end of file