def solution(self, graph):
col = len(graph)
row = len(graph[0])
def bfs(a, b): # y좌표, x좌표
queue = collections.deque()
queue.append([a, b])
# graph[y][x]
while queue:
y, x = queue.popleft()
dy = [1, -1, 0, 0]
dx = [0, 0, 1, -1]
for i in range(4):
ny = y + dy[i]
nx = x + dx[i]
if ny >= col or nx >= row or ny < 0 or nx < 0:
continue
if graph[ny][nx] == 0:
continue
if graph[ny][nx] == 1:
graph[ny][nx] = graph[y][x] + 1
queue.append([ny, nx])
if graph[col-1][row-1] == 1:
return -1
else:
return graph[col-1][row-1]
return bfs(0, 0)