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])
'알고리즘' 카테고리의 다른 글
leetcode 35 - Search Insert Position (0) | 2023.05.28 |
---|---|
백준 2839 - 설탕배달 (python) (1) | 2023.05.21 |
백준 10814 - 나이순 정렬 (class2) (python) (0) | 2023.05.14 |
백준 1181 - 단어정렬 (class2) (python) (0) | 2023.05.12 |
백준 10809 - 알파벳 찾기 (0) | 2023.05.07 |