The whole lesson on one map
Seven stops. Each stop is one CCC pattern — in a contest problem, your job is mostly to recognise which stop you are at.
Down, then Across
up · down · left · right
1 · Coordinates and grid setup
From the notes §1 — grid[row][column], valid when 0 ≤ row < R and 0 ≤ column < C.
Problem — What Is in This Seat?
A seating plan is stored as a grid of characters. A ticket gives you an address: a row and a column. Print the character stored at that address.
The first line gives R C (rows, columns). Then come R lines of the grid. The last line gives the address: row column.
4 5 ..... ..#.. ...S. X.... 2 3
Print the single character at that address.
S
Everything in this section exists to answer this one question — and to never mix up which number walks DOWN and which walks ACROSS. The animation looks up the sample address (2, 3). Predict the output before you step it.
Try it yourself — click any cell
R = len(grid) # number of rows C = len(grid[0]) # number of columns value = grid[row][column] # in the animation: value = grid[2][3] # down 2, across 3 → 'S'
row < R, not row ≤ R.grid[2][3] is S, but grid[3][2] is . — watch the last step of the animation. First index = down. Always.2 · Read and process a grid
From the notes §2 — read one row at a time; turn a string into a list only when you need to change single characters.
Problem — Find Every #
A picture is made of . and # cells. Report the position of every # in the picture, in reading order (top to bottom, left to right).
The first line gives R C. Then come R lines of the grid.
3 3 .#. ..# #..
For each #, print its row and column on its own line, in reading order.
0 1 1 2 2 0
Two jobs hide inside this one question: first read the text lines into a grid, then report the positions. The animation does them in exactly that order — building first, scanning second.
".#."; list(...) cuts it into separate character boxes so each cell can be changed on its own. The trays stack up, in order, into the grid.grid = [list(input().strip())
for _ in range(R)]
# now process it — find every '#'
for r in range(R):
for c in range(C):
if grid[r][c] == '#':
print(r, c)
".#." can be read as row[1] just fine. You only need list(...) when you want to change a character later — strings cannot be changed in place, lists can. Many CCC solutions convert to a list “just in case.”.#. / ..# / #.. — so the program prints the three # positions: 0 1, 1 2, 2 0.3 · Scan every cell
Pattern 1 from the notes §3 — nested loops, for when every location must be checked, counted, or updated.
Problem — Number the Desks
An usher numbers every desk in a room in reading order — left to right along a row, then down to the next row — starting at 1. Print the finished numbering plan.
One line: R C (rows, columns).
3 4
Print R lines: the number stamped on each desk, separated by spaces.
1 2 3 4 5 6 7 8 9 10 11 12
To number the desks, your program must visit every cell exactly once, in exactly this order. That visiting pattern — the scan — is what this section builds. Before you press Play: which desk gets number 7?
for r in range(R): # outer: rows, slow for c in range(C): # inner: cols, fast process(grid[r][c])
4 · The four orthogonal neighbours
Pattern 2 from the notes §3 — up, down, left, right. Validate each coordinate before indexing.
Problem — How Many Neighbours Does This Cell Have?
A cell's neighbours are the cells that share an edge with it — up, down, left, right, never the diagonals. Cells near the border have fewer neighbours, because some of theirs would fall outside the grid. Given the grid size and one cell, print how many neighbours that cell has.
The first line gives R C. The second line gives the cell: r c.
5 5 0 0
Print one number: how many edge-neighbours the cell has.
2
The answer depends on where the cell sits — and you are not allowed to peek outside the grid to count. The animation first stands in the middle at (2, 2), then answers this sample's corner (0, 0). Quick checks once you have the idea: (2, 2) → 4, (0, 2) → 3, (0, 0) → 2. One rule must produce all three.
| Move | (dr, dc) | New cell | What changed? |
|---|---|---|---|
| ↑ Up | (−1, 0) | (r−1, c) | row − 1 |
| ↓ Down | (+1, 0) | (r+1, c) | row + 1 |
| ← Left | (0, −1) | (r, c−1) | column − 1 |
| → Right | (0, +1) | (r, c+1) | column + 1 |
directions = [(1, 0), (-1, 0),
(0, 1), (0, -1)]
for dr, dc in directions:
nr, nc = r + dr, c + dc
if 0 <= nr < R and 0 <= nc < C:
inspect(grid[nr][nc])
if check exists.grid[0][-1] does not always crash — Python reads -1 as the last column. Your program silently inspects the wrong cell on the far side of the grid. The bounds check is not decoration; it is what stops wrap-around.5 · Count adjacent targets — the worked mini-example
Patterns 2 + 3 together, on the notes §4 example: how many X cells share an edge with the centre cell? Diagonals do not count.
Problem — How Many X's Touch the Centre?
You are given a 3 × 3 grid. Count how many of the cells that share an edge with the centre cell contain an X. Cells that touch the centre only at a corner point — the diagonals — do not count.
Three lines, the three rows of the grid.
.X. ... X..
Print one number: the count.
1
This is the student notes' own §4 mini-example, written as a real question. The whole game hides in the words share an edge — they decide which cells you are even allowed to count. Predict before the animation: is the answer 1, or 2?
... / .X. / X.. — that puts an X in the centre cell itself, and the four edge-neighbours are all “.”, so the printed grid’s answer would actually be 0, not 1. The written explanation (“only the cell above is X; the lower-left X is diagonal”) matches the grid below — .X. / ... / X.. — so this animation uses that grid, and its answer really is 1. Worth fixing in the student notes before printing.X scores 1. At the end, sweep the four corners with your eyes and deliberately score them 0 — even the corner that holds an X. The problem statement decides the stencil: “share an edge” = + only.count = 0
for dr, dc in directions:
nr, nc = r + dr, c + dc
if (0 <= nr < R and 0 <= nc < C
and grid[nr][nc] == "X"):
count += 1
# r = 1, c = 1 → count ends at 1
grid[nr][nc] lookup only ever runs on a cell that exists. Order in the and chain is protection, not style.directions list.6 · Build a new grid from old values
Pattern 4 from the notes §3 — for simultaneous updates, calculate into a separate grid so earlier changes do not affect later cells.
Problem — One Turn of Spread
A grid holds . and X cells. In one turn, every . cell that touches an X on any side (edge neighbours only) becomes an X; X cells stay X. All changes happen at the same time. Print the grid after exactly one turn.
The first line gives R C. Then come R lines of the grid.
3 3 ... .X. ...
Print the grid after one turn, R lines.
.X. XXX .X.
The words at the same time are the trap in this question: a cell that becomes X this turn must not spread any further until the next turn. Getting that right is exactly what forces the two-tray method this section teaches.
next_grid = [row[:] for row in grid] # read ONLY from grid # write ONLY into next_grid # ... fill every cell of next_grid ... grid = next_grid # the turn ends: swap
. X . . . left → right and write into the same grid. The cell after X becomes X — and now the next cell sees the X you just wrote, so it becomes X too. Chain reaction: the whole row flips in a single “turn”. That is not a simulation of one step; it is a simulation of five steps squashed into one, and whether it happens depends on loop order. The two-tray rule kills the chain.row[:] matters: next_grid = grid does not copy anything — both names point at the same trays, and writing into one writes into the other. [row[:] for row in grid] builds a fresh list for every row, so the trays really are separate.7 · Trace a path or simulate movement
Pattern 5 from the notes §3 — update row and column using a direction; check whether the move is legal before applying it. This is the engine of the practice problem Snail Path.
Problem — Walk the Token
A token starts on the S cell of a grid that contains walls (#). You are given a string of moves — U up, D down, L left, R right. Apply the moves one at a time: a move that would leave the grid or enter a wall is simply ignored — the token stays where it is. Print the token's final row and column.
The first line gives R C. Then come R lines of the grid. The last line is the move string.
4 5 S..#. ##.#. ..... .##.G DRRRDDRDR
Print the final position: row column.
3 4
That final cell is the goal G — but the token only gets there because two of the nine moves were rejected on the way. This propose → check → step loop is the engine inside real path problems, including Snail Path on the practice page.
nr = r + dr
nc = c + dc
if (0 <= nr < R and 0 <= nc < C
and grid[nr][nc] != "#"):
r, c = nr, nc # step taken
# if the if fails: r, c stay —
# nothing else to write. Staying
# put IS the blocked-move behaviour.
else. The check fails, the assignment never runs, and the token stays. Students often go hunting for the “blocked code” — there isn’t any. The absence of the step is the behaviour.(r, c) pairs — so the program can answer “have I been here before?” The animation numbers your trail ①②③… as you walk.Common mistakes — each one is a broken map
From the notes §5. If you can name the broken picture, you can find the bug in your own code in seconds.
Writing
grid[c][r] reflects the whole map across the diagonal — every cell lands on its mirror twin. Square grids do not even crash; they just answer about the wrong place. Picture: the map flipped over its diagonal fold.Looking at a neighbour before checking it exists: off the edge, or — worse in Python —
-1 wraps to the far side. Picture: stepping off the board and teleporting to the opposite edge.New values leak into decisions that should have used the start-of-turn state. Picture: the chain reaction in §6 — the X that runs across the row.
Using a 3 × 3 square stencil when the problem said “share an edge.” Picture: X stencil instead of + stencil. Re-read the problem’s neighbour rule before you code — §5’s answer flips from 1 to 2 on this alone.
next_grid = grid makes two names for one tray. Picture: two labels taped to the same box. Copy row by row: [row[:] for row in grid].A full scan is O(R × C) and a fixed set of neighbour checks per cell is still O(R × C). For CCC Junior grid sizes that is comfortably fast in Python. Picture: the cost is the number of cells — count cells, not loops.
Practice — which patterns does each problem need?
Pattern 7 (trace a path) is the engine, with pattern 4’s checks before every move, plus a visited trail to track positions and turns.
Patterns 3 + 4: scan a thin two-row grid and reason about neighbours and connected cells — the + stencil on a grid that is almost all edge.