Lessons 21–25 · About 60 min per lesson
Grids, coordinates and boundaries
Express movement and local neighbours with clear coordinate rules.
Lesson breakdown
- Lesson 21
Use row and column positions and distinguish x from row.
- Lesson 22
Apply a sequence of directions to update coordinates.
- Lesson 23
Enumerate four neighbours and check bounds first.
- Lesson 24
Track duplicate visits with a set.
- Lesson 25
Test a boundary start, a return to the origin and a minimal grid.
Worked example
Start at the centre of a 3×3 grid and move right, up, left and down. Count distinct visited cells.
How to solve it
There are 4, including the start. The last step returns to the start and adds no new cell.
Assignment
Start at the upper-left of a 3×3 grid, ignore invalid moves and print the zero-based final position and number of distinct visited cells.
Check your result
Up, right, down, left, up → (0,0) and 4 cells; a lone up move visits 1 cell.