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?
About 30 min · 3 goals · Checked by automated tests; not yet reviewed by an educator
1.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.
2.Predict
A robot at (2, 3) moves 3 right and 4 down. Where is it?
Guess first. Being wrong here is how the surprise works.
3.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.
Grid robot
Coordinates are an address: (x, y) means go x across, then y up, from the origin (0, 0). Drive the robot to the star with the buttons, or focus the grid and use the arrow keys.
Grid
Program so far: (empty)
Robot at (0, 0). Target star at (5, 3).
Can't use the interactive? Read a description instead
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.
4.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
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.
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|.
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.
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.
5.Practice
Practice, not a test. Wrong answers get a hint and another try.
Practice 1 · a fresh version every time
A grid robot starts at (3, 0). It moves: 3 left, 2 right, 2 down, 2 left. What is its x coordinate now? (Up adds to y; right adds to x.)
Practice 2 · a fresh version every time
A warehouse robot can only drive along grid lines (no diagonals). It is at (-3, 3) and must reach (-3, -3). What is the smallest number of one-square moves?
Practice 3
Which program takes a robot from (0, 0) to (2, 1)?
Practice 4
Program A is R R U. Program B is U R R. Starting from the same place, which is true?
6.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?
7.Reflect
Concepts: Coordinate frames and transforms · Vectors