You are viewing a single comment's thread. Return to all comments →
solution in C#:
private static readonly List<(int dx, int dy)> directions = new() { (1, 0), (-1, 0), (0, 1), (0, -1) }; public static int minimumMoves(List<string> grid, int startX, int startY, int goalX, int goalY) { var len = grid.Count; var queue = new Queue<(int cx, int cy, int moves)>(); var visited = new bool[len, len]; queue.Enqueue((startX, startY, 0)); visited[startX, startY] = true; while(queue.Count > 0) { var (cx, cy, moves) = queue.Dequeue(); if(cx == goalX && cy == goalY) { return moves; } foreach(var (dx, dy) in directions) { var (nx, ny) = (cx + dx, cy + dy); while (nx >= 0 && nx < len && ny >= 0 && ny < len && grid[nx][ny] == '.') { if (!visited[nx, ny]) { visited[nx, ny] = true; queue.Enqueue((nx, ny, moves + 1)); } nx += dx; ny += dy; } } } 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 →
solution in C#: