Accel Thinking

CP Weekly Challenges

USACO 2019 January Bronze Shell Game Solution: Simulate Every Starting Position

Track one state variable, simulate all three possible starting shells, and avoid the subtle double-if transition bug.

The only unknown is the pebble’s starting shell. There are just three possibilities, so simulate all three and keep the highest score.

The Core Idea

For each possible start, process every swap in order, update the pebble’s position, count correct guesses, and take the maximum of the three scores.

DSA Map

Item Description
Level USACO Bronze / around CCC J4
Pattern Simulation, state tracking, try all starts
State One integer: the current pebble position
Main lesson Track the smallest sufficient state and respect event order
Next use Shuffles, position tracking, finite-state simulation

Problem Restatement

There are three shells. In each round, shells a and b are swapped, then a guess g is made. The initial pebble position is unknown. Find the maximum score possible over all starting positions.

Minimal State and Transition

Store only position. For swap (a, b), move a to b, move b to a, and leave every other position unchanged. Then compare the updated position with the guess.

The elif matters. Two independent if statements can move a to b and immediately move it back to a.

Python Solution

n = int(input())
rounds = [tuple(map(int, input().split())) for _ in range(n)]

best = 0
for start in range(1, 4):
    position = start
    score = 0
    for a, b, guess in rounds:
        if position == a:
            position = b
        elif position == b:
            position = a
        if position == guess:
            score += 1
    best = max(best, score)

print(best)

C++ Solution

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

struct Round { int a, b, guess; };

int main() {
    int n;
    cin >> n;
    vector<Round> rounds(n);
    for (auto &r : rounds) cin >> r.a >> r.b >> r.guess;

    int best = 0;
    for (int start = 1; start <= 3; start++) {
        int position = start, score = 0;
        for (const auto &r : rounds) {
            if (position == r.a) position = r.b;
            else if (position == r.b) position = r.a;
            if (position == r.guess) score++;
        }
        best = max(best, score);
    }
    cout << best << '\n';
}

Why It Is Correct

The algorithm covers all three possible initial positions. For any fixed start, each transition exactly follows the specified swap and each point is counted after the swap, so that start’s score is correct. The real start is one of the three candidates; therefore, the maximum simulated score is the highest possible score.

Complexity

The algorithm processes N rounds for each of three starts, so the time is O(3N) = O(N). Storing the rounds uses O(N) space.

Common Mistakes

  1. Using two independent if statements instead of if and elif .
  2. Scoring before applying the swap.
  3. Trying only one starting position.
  4. Tracking more state than the problem needs.

Review Questions

  1. Why is one integer enough state?
  2. What exactly fails with two independent if statements?
  3. How would the complexity change with K possible starts?
  4. Which dry-run column exposes an incorrect event order?