You are viewing a single comment's thread. Return to all comments →
from collections import deque def minimumMoves(grid, startX, startY, goalX, goalY): n = len(grid) m = len(grid[0]) directions = [(0, 1), (1, 0), (0, -1), (-1, 0)] queue = deque([(startX, startY, 0)]) visited = set() visited.add((startX, startY)) while queue: x, y, moves = queue.popleft() if (x, y) == (goalX, goalY): return moves for dx, dy in directions: nx, ny = x, y while 0 <= nx + dx < n and 0 <= ny + dy < m and grid[nx + dx][ny + dy] != 'X': nx += dx ny += dy if (nx, ny) not in visited: visited.add((nx, ny)) queue.append((nx, ny, moves + 1)) return -1
Seems like cookies are disabled on this browser, please enable them to open this website
Castle on the Grid
You are viewing a single comment's thread. Return to all comments →