본문 바로가기

boj

백준 2178 미로 탐색

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

 

2178번: 미로 탐색

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

www.acmicpc.net

 

그냥 전형적인 bfs문제

 

bfs를 진행하면서 level까지 업데이트하고,

n-1,m-1에 도달하면, level+1을 출력

 

##코드##

from collections import deque
import sys
input=sys.stdin.readline
def bfs(i=0,j=0):
    arr[i][j]='0'
    s=deque()
    s.append([i,j,1])
    
    while s:
        i,j,v=s.popleft()
        for move in moves:
            a,b=move
            tmp_i=i+a
            tmp_j=j+b
            if 0<=tmp_i<n and 0<=tmp_j<m and arr[tmp_i][tmp_j]=='1':
                # print(tmp_i,tmp_j,v)
                if tmp_i==n-1 and tmp_j==m-1:
                    print(v+1)
                    return
                s.append([tmp_i,tmp_j,v+1])
                arr[tmp_i][tmp_j]='0'

        
n,m=map(int,input().split())
arr=[list(input().strip()) for _ in range(n)]
moves=[[-1,0],[1,0],[0,-1],[0,1]]
bfs()

'boj' 카테고리의 다른 글

백준 17114 하이퍼 토마토  (0) 2022.04.20
백준 7569 토마토2  (0) 2022.04.20
백준 2667 단지번호붙이기  (0) 2022.04.19
백준 13023 ABCDE  (0) 2022.04.18
백준 11724 연결 요소의 개수  (0) 2022.04.17