Moving on a grid through a command sequence.
A row/col position changes step by step in response to a string or array of moves — with each move guarded by bounds checks.
Many AP-style class FRQs model a token, robot, or cursor moving on a 2D grid. The state is two integers (row, col) representing the current position. A method processes a sequence of moves — each move adjusts row or col by 1, but only if the next position is in bounds.
The combined pattern: traverse a sequence (String or array), interpret each move, check bounds before applying. Off-grid moves are typically ignored.
On the AP exam, this pattern appears in robot-on-grid FRQs and in maze-traversal questions.
Decode each command; check bounds; update position.
Three operations per move.
Class skeleton
public class Robot {
private int row, col;
private int rows, cols; // grid dimensions
public Robot(int rows, int cols) {
this.rows = rows;
this.cols = cols;
this.row = 0;
this.col = 0;
}
public void move(char direction) {
int newRow = row;
int newCol = col;
if (direction == 'U') newRow--;
else if (direction == 'D') newRow++;
else if (direction == 'L') newCol--;
else if (direction == 'R') newCol++;
if (newRow >= 0 && newRow < rows &&
newCol >= 0 && newCol < cols) {
row = newRow;
col = newCol;
}
// out-of-bounds moves are ignored
}
}
Processing a command sequence
public void runCommands(String commands) {
for (int i = 0; i < commands.length(); i++) {
move(commands.charAt(i));
}
}
Each char is one move; the loop processes them in order.
Why compute newRow/newCol first?
Checking bounds before mutating row/col means out-of-bounds moves leave the position unchanged. Mutating first and then "un-doing" is error-prone.
Grid-movement vocabulary.
Used in robot and maze problems.
- Position state — current row and column.
- Command sequence — String or array of moves to process.
- Direction — one of U, D, L, R (or similar).
- Bounds check — verifying the next position is valid.
- Trial move — computing next position before committing.
- Ignored move — out-of-bounds move that leaves state unchanged.
How grid movement is tested.
Bounds checking before mutation dominates.
- Check bounds before committing. Compute trial position; verify; then assign.
- Bound:
0 <= row < rows. Same for columns. - Ignored moves are common. Don't crash; just skip.
- Process commands in order. Sequence matters.
MCQ patterns: trace final position after a sequence with some illegal moves.
Trace a Robot through commands.
Some moves get ignored.
Setup. 3×3 grid. Robot r = new Robot(3, 3); — starts at (0, 0).
Commands: "RRDDLU"
- R: try (0, 1). In bounds → row=0, col=1.
- R: try (0, 2). In bounds → row=0, col=2.
- D: try (1, 2). In bounds → row=1, col=2.
- D: try (2, 2). In bounds → row=2, col=2.
- L: try (2, 1). In bounds → row=2, col=1.
- U: try (1, 1). In bounds → row=1, col=1.
Final position: (1, 1).
What if commands were "UUUR"? First U: try (-1, 0); out of bounds → ignored. Same for next two U's. R: try (0, 1); in bounds → row=0, col=1. Final: (0, 1).
Grid-movement pitfalls.
Most miss the bounds check or apply it after mutation.
- Mutating row/col before checking bounds. Causes out-of-grid state.
- Off-by-one in bounds. Valid row is
0torows - 1, notrows. - Forgetting one direction. "U" without "D" — or missing "L"/"R".
- Using
.equals()on chars. Use==. - Confusing row with col. Movement up changes row; left changes col.
- Throwing exceptions on invalid moves. AP spec usually says ignore.
Direction deltas.
How each command shifts row/col.
Grid Movement Reference
AP Quick Reference| Command | Row delta | Col delta |
|---|---|---|
| U (up) | -1 | 0 |
| D (down) | +1 | 0 |
| L (left) | 0 | -1 |
| R (right) | 0 | +1 |
| Valid range | 0 <= row < rows | 0 <= col < cols |
How to master grid movement.
Trial first; check second; commit third.
Use the three practice tools below this lesson in order:
- MCQ Practice — sketch the grid; trace one move at a time; verify each bounds check.
- Java Lab — build a Robot with move and runCommands. Test sequences that hit walls in every direction.
- FRQ Practice — write the trial-then-check-then-commit pattern. Don't mutate before validation.
If you can sketch the path on paper before coding, the bounds and direction logic falls into place quickly.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.
AP CSA Unit 10 Topic 03 Grid Movement with Bounds and Sequence Processing FRQ Practice
AP-style topic practice assessment