๋ฐ์ํ
๐ป ๋ฐฑ์ค 2178๋ฒ [๋ฏธ๋กํ์]
ํ์ด)
# ์ต์ข
์ ์ถ
from collections import deque
n,m = map(int,input().split())
miro = [list(map(int,input())) for _ in range(n)]
visited = [[0]*m for _ in range(n)]
dis = [[0]*m for _ in range(n)]
q = deque([[0,0]])
visited[0][0] = 1
dis[0][0] = 1
while q :
x,y = q.popleft()
for [i,j] in [[x+1,y],[x,y+1],[x-1,y],[x,y-1]] :
if 0 <= i <= n-1 and 0 <= j <= m-1 :
if (visited[i][j] == 0) and (miro[i][j] == 1):
q.append([i,j])
visited[i][j] = 1
dis[i][j] = dis[x][y] + 1
print(dis[n-1][m-1])
# ==> ๋ฉ๋ชจ๋ฆฌ : 32144KB, ์๊ฐ : 120ms, ์ฝ๋๊ธธ์ด : 553B
๋ฉ๋ชจ๋ฆฌ | ์๊ฐ | ์ธ์ด | ์ฝ๋๊ธธ์ด |
32144KB | 120ms | Python 3 | 553B |
728x90
๋ฐ์ํ
'๋ฐฑ์ค & ํ๋ก๊ทธ๋๋จธ์ค > DFS & BFS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค] DFS/BFS level2 ํ๊ฒ๋๋ฒ (0) | 2022.03.24 |
---|---|
[๋ฐฑ์ค 1012] [dfs/bfs] ์ ๊ธฐ๋ ๋ฐฐ์ถ (0) | 2021.04.29 |
[๋ฐฑ์ค 1012] [dfs/bfs] ์ ๊ธฐ๋ ๋ฐฐ์ถ (0) | 2021.04.24 |
[๋ฐฑ์ค 2667] [dfs/bfs] ๋จ์ง๋ฒํธ๋ถ์ด๊ธฐ (0) | 2021.04.23 |
[๋ฐฑ์ค 1260] [dfs/bfs] BFS์ DFS โ (0) | 2021.04.23 |
[๋ฐฑ์ค 2606] [dfs/bfs] ๋ฐ์ด๋ฌ์ค (0) | 2021.04.22 |
[๋ฐฑ์ค 2331] [dfs] ๋ฐ๋ณต์์ด (0) | 2021.04.22 |