Videos

Video

Shell Game

Read the guide

Simulate all three starting positions and score each guess after the swap. Trace the official sample step by step.

Watch on YouTube

Track the pebble’s current position. Replay the same log independently for each of the three starting positions, then take the highest score.

Update the position before scoring

A row a b g means swap the shells at positions a and b, then guess position g. Move the pebble to the opposite position if it was under one of those shells. Otherwise it stays where it is. Score the guess after the swap.

Trace all three starts

The official sample swaps 1↔2, 3↔2 and 1↔3. Every guess is 1. The positions below are the positions after each swap.

Starting position Round 1 Round 2 Round 3 Correct guesses
1 2 3 1 1
2 1 1 3 2
3 3 2 2 0

The answer is 2. From start 2, the pebble stays at 1 in round two because the swap only touches 2 and 3.

Test the two common mistakes

  • Use if / elif so a move from 1 to 2 cannot trigger a second move back to 1 in the same round.
  • Reset the score and replay the entire log for every starting position. Three passes over N rounds take O(N) time.

For a one-round test, use 1 2 2. Starting at 1 should move the pebble to 2 and score one point. This historical problem specifies shell.in and shell.out.

Official USACO problem and sample · track both sides of a pour.