from collections import deque
def solution(grid):
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
visited = [[False] * 5 for _ in range(5)]
dist = [[0] * 5 for _ in range(5)]
queue = deque()
queue.append((0, 0))
visited[0][0] = True
dist[0][0] = 1
while queue:
x, y = queue.popleft()
if x == 4 and y == 4:
return dist[x][y]
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0 <= nx < 5 and 0 <= ny < 5:
if grid[nx][ny] == 0 and not visited[nx][ny]:
visited[nx][ny] = True
dist[nx][ny] = dist[x][y] + 1
queue.append((nx, ny))
return -1
grid = [
[0, 1, 0, 0, 0],
[0, 1, 0, 1, 0],
[0, 0, 0, 1, 0],
[1, 1, 1, 1, 0],
[0, 0, 0, 0, 0]
]
print(solution(grid))
To embed this project on your website, copy the following code and paste it into your website's HTML: