-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboj_02178.py
More file actions
33 lines (25 loc) · 767 Bytes
/
boj_02178.py
File metadata and controls
33 lines (25 loc) · 767 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 2022.02.27
# Dongyoung Kwon @Chuncheonian (ehddud2468@gmail.com)
# https://www.acmicpc.net/problem/2178
from collections import deque
n, m = map(int, input().split())
matrix = [list(map(int, input())) for _ in range(n)]
dr = [0, -1, 0, 1]
dc = [1, 0, -1, 0]
def bfs(x, y):
queue = deque()
queue.append((x, y))
while queue:
x, y = queue.popleft()
for i in range(4):
nx = x + dr[i]
ny = y + dc[i]
if nx < 0 or nx >= n or ny < 0 or ny >= m:
continue
if matrix[nx][ny] == 0:
continue
if matrix[nx][ny] == 1:
queue.append((nx, ny))
matrix[nx][ny] = matrix[x][y] + 1
return matrix[n-1][m-1]
print(bfs(0, 0))