
CP Weekly Challenge
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.
竞赛编程每周挑战
USACO 2019 January Bronze Shell Game 题解:枚举每一种初始位置
用一个状态变量模拟三种初始位置,并避开两个独立 if 造成的状态更新错误。
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
- Using two independent
ifstatements instead ofifandelif. - Scoring before applying the swap.
- Trying only one starting position.
- Tracking more state than the problem needs.
Review Questions
- Why is one integer enough state?
- What exactly fails with two independent
ifstatements? - How would the complexity change with
Kpossible starts? - Which dry-run column exposes an incorrect event order?
三只杯子不断交换,猜测也在每次交换后发生。真正的不确定只有一个:石子最开始在哪只杯子下面。既然初始位置只有 1、2、3 三种,就把三种情况全部模拟一遍,取最高得分。
一句话结论
对每个可能的初始位置,按输入顺序更新石子位置并统计猜对次数;三个分数的最大值就是答案。
这道题在 DSA Map 中的位置
| 项目 | 内容 |
|---|---|
| Level | USACO Bronze / CCC J4 附近 |
| Pattern | Simulation、state tracking、try all starts |
| Prerequisite | 条件判断、循环、列表 |
| What this teaches | 用一个变量保存最小充分状态,并严格按事件顺序更新 |
| Where it appears again | 位置追踪、洗牌、有限状态机、多种初始状态 |
下一步推荐
- 阅读《Dry Run 是什么?》,用状态表检查每一步更新。
- 阅读《什么才是一个好的 Debugging Question?》,学会描述“期望状态”和“实际状态”。
- 再做 USACO Bronze Mixing Milk,比较“单个位置状态”和“数组状态”。
题意
桌上有三只杯子,石子最初藏在其中一只下面。每轮先交换杯子 a 和 b,再猜石子是否在杯子 g 下。初始位置未知,要求输出在最有利的初始位置下,最多能猜对多少次。
样例:
3
1 2 1
3 2 1
1 3 1
输出:
2
状态应该保存什么?
我们不需要保存三只杯子的完整排列。题目只关心石子在哪,因此一个整数就足够:
position = 当前石子所在杯子
处理交换 (a, b) 时:
- 如果
position == a,石子跟到b; - 如果
position == b,石子跟到a; - 否则石子位置不变。
更新后再比较 position == g。顺序不能反,因为猜测发生在交换之后。
为什么要枚举三种起点?
初始位置未知,但候选集合只有:
1, 2, 3
对每个起点都完整模拟,得到一个分数。真实起点必然属于这三种情况,所以最大分数一定被计算到。
Dry Run:样例如何得到 2?
| 起点 | 第 1 轮后 | 第 2 轮后 | 第 3 轮后 | 得分 |
|---|---|---|---|---|
| 1 | 2,猜 1 错 | 3,猜 1 错 | 1,猜 1 对 | 1 |
| 2 | 1,猜 1 对 | 1,猜 1 对 | 3,猜 1 错 | 2 |
| 3 | 3,猜 1 错 | 2,猜 1 错 | 2,猜 1 错 | 0 |
最大值是 2。
一个很隐蔽的更新错误
下面写法是错的:
if position == a:
position = b
if position == b:
position = a
如果 position 原来等于 a,第一个 if 把它改成 b,第二个 if 又立刻成立,于是它被改回 a。一次交换被执行了两次判断。
正确写法必须用 elif:
if position == a:
position = b
elif position == b:
position = a
Python 参考实现
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++ 参考实现
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
struct Round {
int a, b, guess;
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<Round> rounds(n);
for (Round &r : rounds) {
cin >> r.a >> r.b >> r.guess;
}
int best = 0;
for (int start = 1; start <= 3; start++) {
int position = start;
int score = 0;
for (const Round &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';
}
正确性证明
算法分别模拟石子从 1、2、3 开始的情况,因此覆盖了所有可能初始状态。固定一个起点后,每轮根据交换的两个杯子准确更新石子位置,并在交换后按题意计分,所以该起点的模拟得分正确。真实起点必然是三者之一,取三个正确得分的最大值,得到的就是最高可能分数。
复杂度
每个起点处理 N 轮,共三个起点:
Time: O(3N) = O(N)
Space: O(N) for stored rounds, or O(1) extra with three parallel states
常见错误
- 用两个独立
if更新位置,导致一次交换把位置改回去。 - 先计分再交换,弄反事件顺序。
- 只假设一个初始位置,而没有尝试 1、2、3。
- 追踪整套杯子排列,增加了不必要的状态。
- 每轮重置
score或position,丢失历史状态。
复习问题
- 为什么一个整数足以描述当前状态?
- 为什么两个独立
if会出错? - 如果有
K个可能起点和N轮,复杂度是多少? - 怎样改成不保存全部 rounds、只扫描一次输入?
- Dry Run 表中哪一列最容易暴露“先猜后交换”的错误?
Questions or feedback?
Have a question about this article or want to suggest an improvement? Send us a private message.
有问题或建议?
如果你对本文有疑问,或希望提出改进建议,请给我们发送私密留言。