Lite / Discovering How the World Works / Week 13
Programming a robot on a grid
A warehouse robot can only drive along the aisles. What's the shortest route to a shelf, and how many routes are that short?
Why should I care?
Real warehouse robots, delivery bots and even game characters move on grids. Writing their instructions means thinking in changes of position, and checking that a list of moves really ends where you meant.
- Describe a move as a change in x and a change in y.
- Predict a robot's final position from a list of moves.
- Find the smallest number of grid moves between two points.
Predict
A robot at (2, 3) moves 3 right and 4 down. Where is it?
- (5, 7)
- (5, −1)
- (−1, 7)
Guess first, then reveal
(5, −1). Right adds to x: 2 + 3 = 5. Down subtracts from y: 3 − 4 = −1. It has gone below the x-axis, into negative y.
Discover
For each target, first write your full program (like R R R U U) on paper, then type it in with the buttons. Did you reach the star in the shortest number of moves? Find two different shortest programs for the same star.
What the interactive shows (text version)
In the grid robot diagram, each move changes one coordinate by 1, and the readout lists every move made so far, for example 'R R U'. When the robot reaches the star, it reports how many moves it used and the shortest possible number, which is the across gap plus the up/down gap.
Understand
A move is a change
Each instruction changes the coordinates. Right: x + 1. Left: x − 1. Up: y + 1. Down: y − 1. A move like '3 right, 2 up' is a change of (+3, +2). Engineers call a change in position a displacement, and they often draw it as an arrow.
Track x and y separately
To predict where a long program ends, add up all the x changes and all the y changes separately. R R U L U R: x changes +1 +1 −1 +1 = +2, y changes +1 +1 = +2. From (0, 0) the robot ends at (2, 2), however the moves are mixed up.
Shortest route
On a grid with no diagonal moves, the smallest number of moves from A to B is (the across gap) + (the up/down gap). From (1, 2) to (4, 6): 3 across + 4 up = 7 moves. There are usually many different 7-move routes, because you can mix the order. A good robot planner picks one that avoids obstacles.
Explain it another way
Intuitive
Think of walking around a city of square blocks. To get 3 blocks east and 4 blocks north you must walk 7 blocks, whichever streets you choose.
Technical
A displacement is a vector (Δx, Δy). Displacements add component by component, so the order of moves doesn't change the final position. Grid ('taxicab' or Manhattan) distance is |Δx| + |Δy|.
Mathematical
From (x₁, y₁) to (x₂, y₂): Δx = x₂ − x₁, Δy = y₂ − y₁, moves = |Δx| + |Δy|. From (−2, 3) to (3, −1): Δx = 5, Δy = −4, so 9 moves.
Applied
Warehouse robots plan on a grid of floor squares. Their software checks the shortest route, then picks the one that doesn't cross another robot's path at the same time.
Worked example 1
A robot runs: U U R R R D L. It started at (1, 1). Where does it end?
x: +3 (three R) −1 (one L) = +2, so x = 1 + 2 = 3. y: +2 (two U) −1 (one D) = +1, so y = 1 + 1 = 2. It ends at (3, 2).
Common mix-ups
Different orders of the same moves end in different places.
✓ The end point depends only on the total x change and total y change. The path in between can differ, which matters for obstacles.
Practice
Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.
Apply it
Draw a 6 × 6 grid with three 'shelf' squares the robot can't enter. Write the shortest program from (0, 0) to (5, 5) that avoids them. Is it still 10 moves?
Reflect
Why might a real robot NOT choose the shortest route?
Write your answer on paper or in your own notes. Lite never stores what you write.