← 返回 bloomberg 的题目列表Enumerate Round-Robin Match Schedules (Generate All Possible Pairings by Round)
类型:online_judge
There are n teams labeled 1..n that need to play a round-robin schedule:
There are n-1 rounds when n is even, or n rounds when n is odd (introduce a bye each round).
In each round, each team plays at most one match.
Every pair of distinct teams plays exactly once (for odd n, one team has a bye each round, but every pair still meets exactly once).
Task: Generate all possible schedules (each schedule is a list of rounds, each round is a list of pairings (a,b)).
Output format
A list of schedules; each schedule is:
list of rounds;
each round: list of matches; each match is a pair (a,b).
Example
For n = 4, one valid schedule is:
[
[ (1,2), (3,4) ],
[ (1,3), (2,4) ],
[ (1,4), (2,3) ]
]
Constraints
1 <= n <= 8 (assume small n since we must enumerate all solutions)
The order of matches within a round and the order within each pair do not matter; avoid trivial duplicates.
Must handle both even and odd n (for odd n, use a dummy team 0 as a bye).
Example
Input
4
Output
(output is a JSON list of schedules; one valid schedule includes [[(1,2),(3,4)],[(1,3),(2,4)],[(1,4),(2,3)]] )