This problem looks like a standings calculation, but its real question is about the future: how can we count every possible set of remaining results without missing or duplicating one?
Treat each unplayed game as a three-way decision: the first team wins, the second team wins, or the game is tied. There are at most six unplayed games, so the complete search tree has at most 3^6 = 729 leaves. A direct recursive search is clear, reliable, and easily fast enough.
The Core Idea
Compute the points from games already played, list the remaining games, try all three outcomes for each one, and count leaves where the favourite team finishes strictly above all three opponents.
Where This Fits in the CCC DSA Map
| Item | Description |
|---|---|
| Level | CCC Junior J5 / introductory Senior S3 search |
| Pattern | Complete search, recursive enumeration, apply-recurse-undo |
| Prerequisites | Lists, sets, functions, recursion, exponential growth |
| What this teaches | Translate “all possibilities” into a state tree and prove coverage |
| Where it appears again | Permutations, backtracking, game states, constraint search, dynamic programming |
What to Study Next
- Read “Why Should You Write Brute Force First?” to practise defining a complete search space.
- Compare this recursive decision tree with the interval loops in Daisy Chains.
- Later, study CCC 2015 J5 Pi Day to see how repeated states lead from search to dynamic programming.
Problem Restatement
Four teams play a six-game round robin. A win gives 3 points to the winner, a loss gives 0, and a tie gives 1 point to each team. Some games have already been played. Count how many assignments of results to the remaining games make the favourite team finish with strictly more points than every other team. A tie for first is not enough.
The six possible games are:
(1,2) (1,3) (1,4) (2,3) (2,4) (3,4)
Price the Search Before Coding
If G games have been played, then R = 6 - G remain. Each remaining game has three possible point outcomes, so there are 3^R complete futures. In the worst case, R = 6, giving only 729 leaves.
That small bound is the key constraint. Complete search is not a desperate fallback here; it is the intended shape of a dependable solution.
Define the State
The future depends only on:
- the index
iof the next remaining game; - the current point totals of the four teams.
We can write the state as (i, p1, p2, p3, p4). Two paths that reach the same state have exactly the same possible completions, so memoization is safe.
The Three Branches
For a current game (a, b), try (3,0), (0,3), and (1,1). Each branch follows the same discipline:
apply the points -> recurse -> undo the points
The undo step matters. Without it, one branch changes the starting state of the next branch.
Terminal Test and Safe Pruning
At a leaf, the favourite must have more points than every opponent. Use strict > because a tie for first does not count.
We can also reject a state early. If the favourite has p points and appears in q remaining games, its final score is at most p + 3q. If an opponent already has at least that many points, the favourite can never finish strictly first. The comparison is >=, because equality would still be only a tie.
Python and C++ Implementations
The English page includes complete standalone implementations as well. These baseline versions keep the same remaining-game model and apply-recurse-undo search, without requiring memoization to fit the official limits.
Python
favourite = int(input())
played_count = int(input())
all_games = [
(1, 2), (1, 3), (1, 4),
(2, 3), (2, 4), (3, 4)
]
points = [0] * 5
played = []
# Process games already played
for i in range(played_count):
a, b, score_a, score_b = map(int, input().split())
played.append((min(a, b), max(a, b)))
if score_a > score_b:
points[a] += 3
elif score_b > score_a:
points[b] += 3
else:
points[a] += 1
points[b] += 1
# Find remaining games
remaining = []
for game in all_games:
if game not in played:
remaining.append(game)
# Start with current standings
states = [points]
# Simulate all possible outcomes
for a, b in remaining:
new_states = []
for state in states:
# Team A wins
p = state.copy()
p[a] += 3
new_states.append(p)
# Team B wins
p = state.copy()
p[b] += 3
new_states.append(p)
# Tie
p = state.copy()
p[a] += 1
p[b] += 1
new_states.append(p)
states = new_states
# Count outcomes where favourite wins outright
answer = 0
for state in states:
wins = True
for team in range(1, 5):
if team != favourite and state[team] >= state[favourite]:
wins = False
break
if wins:
answer += 1
print(answer)
C++
#include <iostream>
using namespace std;
int allGames[6][2] = {
{1, 2}, {1, 3}, {1, 4},
{2, 3}, {2, 4}, {3, 4}
};
int favourite, playedCount;
int points[5];
int remainingGames[6][2];
int remainingCount;
int search(int i) {
if (i == remainingCount) {
for (int team = 1; team <= 4; team++) {
if (team != favourite && points[favourite] <= points[team]) {
return 0;
}
}
return 1;
}
int a = remainingGames[i][0];
int b = remainingGames[i][1];
int total = 0;
points[a] += 3;
total += search(i + 1);
points[a] -= 3;
points[b] += 3;
total += search(i + 1);
points[b] -= 3;
points[a]++;
points[b]++;
total += search(i + 1);
points[a]--;
points[b]--;
return total;
}
int main() {
cin >> favourite >> playedCount;
bool played[5][5] = {};
for (int i = 0; i < playedCount; i++) {
int a, b, scoreA, scoreB;
cin >> a >> b >> scoreA >> scoreB;
played[a][b] = true;
if (scoreA > scoreB) points[a] += 3;
else if (scoreB > scoreA) points[b] += 3;
else {
points[a]++;
points[b]++;
}
}
for (auto &game : allGames) {
if (!played[game[0]][game[1]]) {
remainingGames[remainingCount][0] = game[0];
remainingGames[remainingCount][1] = game[1];
remainingCount++;
}
}
cout << search(0) << '\n';
}
Why the Algorithm Is Correct
The remaining-game list contains every unplayed pair exactly once. At each level the search tries all three legal outcomes for the next game, so every complete future appears exactly once. The leaf test matches the statement’s strictly-first rule. Memoization combines only states with identical remaining games and scores, and the ceiling prune removes only states whose best possible finish already fails. Therefore the returned count is exactly the number of winning result assignments.
Complexity
With R remaining games, the baseline search takes O(3^R) time and O(R) recursion space. Since R <= 6, there are at most 729 leaves. Memoization and pruning reduce the actual work further but are not required to make the official limits feasible.
Common Mistakes
- Accepting a tie for first by using
>=at the leaf. - Forgetting to undo one or both score changes after recursion.
- Trying only two outcomes and omitting ties.
- Adding the raw game score instead of tournament points.
- Using
>instead of>=in the ceiling prune.
Review Questions
- Why are there at most
3^6leaves? - Why must the state include both the game index and all four point totals?
- What goes wrong if an update is not undone?
- Why does the leaf use
>while the prune uses>=? - Why might the same search become too large with eight teams?
Questions or feedback?
Have a question about this article or want to suggest an improvement? Send us an email.
