You are viewing a single comment's thread. Return to all comments →
@kastianoza is absolutely right about poor quality of problem description. The author should pay me back hakkos spent on case analysis.
here code in python for better understanding for those who have problems with some corner cases
def minimumMoves(grid, startY, startX, goalY, goalX): Y, X = len(grid), len(grid[0]) t = [[float('inf')]*X for _ in range(Y)] t[startY][startX] = 0 sys.setrecursionlimit(10001) def rec(qs): #print(t) if not qs: return t[goalY][goalX] nqs = set() for qx, qy in qs: if qx == goalX and qy == goalY: return t[qy][qx] if t[qy][qx] < float('inf') else -1 for dx, dy in [(1, 0), (0, 1), (-1, 0), (0, -1)]: nx, ny = qx, qy while 0 <= nx+dx < X and 0 <= ny+dy < Y and grid[ny+dy][nx+dx] != 'X': nx += dx ny += dy if t[ny][nx] <= t[qy][qx] + 1: continue t[ny][nx] = t[qy][qx] + 1 nqs.add((nx, ny)) return rec(nqs) return rec(set([(startX, startY)]))
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 →
@kastianoza is absolutely right about poor quality of problem description. The author should pay me back hakkos spent on case analysis.
here code in python for better understanding for those who have problems with some corner cases