You are given an origin point and a list of vectors. The output is a recursively built list of coordinates that expands outward from the origin.
Each vector creates two branches: one at origin + vector and one at origin - vector. Between those two branch points, you place the full result built from the remaining vectors.
That is why origin = [0,0] and vectors = [[2,1]] gives [[2,1],[0,0],[-2,-1],[0,0]]: first the positive branch, then the base case at the origin, then the negative branch, then the base case again. With origin = [1,-1] and vectors = [[3,0],[0,2]], the same pattern repeats one level deeper.
If there are no vectors left, the result is just [origin]. So the task is to generate the full recursive signal map in that exact order.
Example Input & Output
Example with input: origin = [0,0], vectors = []
Example with input: origin = [0,0], vectors = [[2,1]]
Example with input: origin = [1,-1], vectors = [[3,0], [0,2]]
Algorithm Flow

Solution Approach
This pattern is easiest to build with recursion.
Suppose we are processing vector index i. If i is already past the end of the list, return a list containing only the origin.
Otherwise let the current vector be [dx, dy]. Build the answer as:
The key detail is that the recursive result for the remaining vectors is inserted after both the positive point and the negative point, so the same smaller pattern appears on both sides.
If there are m vectors, the output size grows exponentially because each level duplicates the rest of the structure.
Best Answers
import java.util.*;
class Solution {
public int find_shortest_signal_path(int[][] grid) {
int rows = grid.length;
if (rows == 0) return -1;
int cols = grid[0].length;
if (grid[0][0] == 1 || grid[rows-1][cols-1] == 1) return -1;
Queue<int[]> q = new LinkedList<>();
q.add(new int[]{0, 0, 1});
boolean[][] visited = new boolean[rows][cols];
visited[0][0] = true;
int[][] dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
while (!q.isEmpty()) {
int[] curr = q.poll();
int r = curr[0], c = curr[1], d = curr[2];
if (r == rows - 1 && c == cols - 1) return d;
for (int[] dir : dirs) {
int nr = r + dir[0], nc = c + dir[1];
if (nr >= 0 && nr < rows && nc >= 0 && nc < cols && grid[nr][nc] == 0 && !visited[nr][nc]) {
visited[nr][nc] = true;
q.add(new int[]{nr, nc, d + 1});
}
}
}
return -1;
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
