A cell has at most four edges, so a search that touches each cell and edge once is O(RC)
You never build an edge list. Four offsets and a bounds check produce a cell's edges when you ask for them. Here are all the edges of that 3 by 4 grid:
Predict
A 3 by 4 grid. How many edges join two cells that share a side?
Every edge of a 3 by 4 grid
3 edges in each of 3 rows, and 4 in each of the 2 gaps between rows: 17 edges for 12 cells.
Count them once, because every cost in this lesson comes from this count. An R×C grid has R(C−1) edges inside its rows: C−1 in each of the R rows. It has (R−1)C edges between its rows: C in each of the R−1 gaps. That's fewer than 2RC, two per cell.
A search that touches every node and every edge once costs O(V+E), where V is the number of nodes and E the number of edges. On a grid, V is RC and E is under 2RC. So that search is O(RC): linear in the number of cells.
In the editor, write count_edges(grid): the number of edges, pairs of open cells that share a side. Count each one once, and leave out rocks #.
Pick an answer above to see the rest
Instructions
1
Write count_edges(grid): the number of pairs of open cells, not #, that share a side.
2
From each open cell, look only right and down, so each edge is counted once.