Loading…
Loading…
Explores the grid in expanding rings from the start, guaranteeing the fewest-steps path on an unweighted grid.
Enqueue the start node.
1Queue<Integer> q = new ArrayDeque<>();2q.add(start); visited[start] = true;3while (!q.isEmpty()) {4 int node = q.poll();5 if (node == goal) return buildPath();6 for (int nb : neighbors(node))7 if (!visited[nb]) {8 visited[nb] = true; parent[nb] = node;9 q.add(nb);10 }11}