알고리즘

백준 2178 - 미로탐색 (python)

Jaymyong66 2023. 5. 21. 20:28

https://www.acmicpc.net/problem/2178

 

2178번: 미로 탐색

첫째 줄에 두 정수 N, M(2 ≤ N, M ≤ 100)이 주어진다. 다음 N개의 줄에는 M개의 정수로 미로가 주어진다. 각각의 수들은 붙어서 입력으로 주어진다.

www.acmicpc.net

N, M = map(int, input().split())

miro = []

for i in range(N):
  miro.append(list(input()))


miro[0][0] = 1


dx = [-1,1,0,0]
dy = [0,0,-1,1]

q = [[0,0]]

while q:
  x, y = q[0][0], q[0][1]

  del q[0]

  for i in range(4):
    nx = x+dx[i]
    ny = y+dy[i]

    if nx>=0 and ny>=0 and nx<N and ny<M :
      if miro[nx][ny] == "1":
        q.append([nx,ny])
        miro[nx][ny] = miro[x][y] + 1

print(miro[N-1][M-1])