We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Castle on the Grid
Castle on the Grid
Sort by
recency
|
330 Discussions
|
Please Login in order to post a comment
from collections import deque
def minimumMoves(grid, startX, startY, goalX, goalY): n = len(grid) directions = ( (-1, 0), (0, 1), (1, 0), (0, -1), ) queue = deque( [(startX, startY, 0)] ) visited = set( [(startX, startY)] )
simply O(n^3) algo
Python easy implementation using deque:
import os from collections import deque
def minimumMoves(grid, startX, startY, goalX, goalY): # Define the directions for up, down, left, right movements directions = [(1, 0), (-1, 0), (0, 1), (0, -1)]
if name == 'main': fptr = open(os.environ['OUTPUT_PATH'], 'w')
deque helps in improving time complexity to O(1) ... while append and pop the points/elements.