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
|
333 Discussions
|
Please Login in order to post a comment
It would be helpful if the description specified whether or not one MUST go until you hit a wall or the edge (think video game ice grid puzzles) or if the player can optionall stop short (think rook in chess). The name of this gives a hint for those familiar with chess, but I do not think it is obvious from the problem description alone to someone unfamiliar with that game.
Solution in C using BFS algorithm
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