Why breadth-first search finds a shortest route
The grid is an unweighted graph: each open cell is a vertex and each legal four-direction move is an edge of equal cost. Breadth-first search (BFS) explores a FIFO queue by distance layers. It marks a cell when first discovered and stores its predecessor; therefore the first discovered route to the goal has the fewest moves. Here the robot starts at index 24 and the goal is index 5; blocked cells are excluded. Once found, predecessor links are followed backward to reconstruct the route, then Move one step advances the displayed robot.
queue ← [start]; parent[start] ← none
while queue not empty:
u ← pop front
for each open neighbor v of u:
if v unseen: parent[v] ← u; enqueue(v)
reconstruct goal-to-start using parentComplexity and assumptions
Let V=WH be the number of grid cells and E the number of legal neighbor links. BFS takes O(V+E) time and O(V) auxiliary space for the queue and predecessor map; on a four-neighbor grid E=O(V), so both are O(WH). Route reconstruction and route-set construction take O(V) time and space in the worst case. The visible grid is O(V) DOM memory, separate from BFS workspace. The BFS bound assumes an unweighted static map and a FIFO queue; movement is assumed perfect, so a real robot needs feedback and replanning when its pose or obstacles differ. Each Move one step redraws the entire grid in O(V) visualization time, so showing a route of up to O(V) moves can take O(V²) total UI work even though the route search itself is O(V+E).