Grid Traversal
Step 1 of 10
A grid is a list of rows, so grid[r][c] is row r, column c — row first, every time.
The Idea
A grid is a list of rows, so grid[r][c] reads row r, column c. Row first, always.
Neighbours are offsets added to that pair: four for edge-sharing, eight if diagonals count. Each one needs the same guard, 0 <= r < rows and 0 <= c < cols, because Python answers a negative index with the far side of the grid instead of an error.
Real-World Example
A spreadsheet recalculates exactly this way. Changing one cell walks the neighbours that referenced it, and the app has to know the sheet's edges — otherwise a formula in row 1 would quietly pull its value from the bottom of the sheet.
The Code
Your turn
Put the steps in the right order.
- Add the offset to the current row and column
- Take the next offset from the direction list
- Read the cell — it is safely inside the grid
- Reject the candidate unless both indexes are in range
Mini quiz
1 / 3