๋ฐฑ์ค€ & ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค/DFS & BFS

[๋ฐฑ์ค€ 2178] [dfs/bfs] ๋ฏธ๋กœํƒ์ƒ‰

_cactus 2021. 4. 23. 22:05
๋ฐ˜์‘ํ˜•

๐Ÿ’ป ๋ฐฑ์ค€ 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
๋ฐ˜์‘ํ˜•