我的任务是为公司的团队比赛制定时间表。最初我认为这将是非常微不足道的,但我在想出一个有效的解决方案时遇到了一些困难。以下是需要满足的事实:
这不是循环赛,因为所有球队都不会互相比赛。这有点类似于瑞士锦标赛,但是有多种游戏类型的限制,所有球队都必须参加。我正在努力找出正确的算法来确定这个时间表,任何有助于解决问题的帮助或信息都会很棒。
我认为你可以使用本地搜索,但是让我给你一个数学结构。
将16支队伍分成8支A队和8 B队,每支A队只打2 B队,每支B队只打2支A队。
这种结构需要两个相互正交的8x8拉丁正方形,例如,
abcdehfg
badchegf
cdabgfhe
dcbafgeh
ehgfabdc
hefgbacd
fghedcab
gfehcdba
abcdefgh
cdabhgfe
efhgabdc
hgefcdba
dcbaghef
badcfehg
feghbacd
ghfedcab
用A队来索引行,用B队来索引列。第一个拉丁方块的条目指定A队将与B队竞争的活动。其中两个字母被视为Rest。根据拉丁方块的属性,每个团队只完成一次活动(Rest两次)。
第二个拉丁方块的条目表示A队何时与B队比赛(或双方都Rest)。根据拉丁方块的性质,每个队在每轮中做一件事。根据相互正交的拉丁方块的性质,每个活动在每轮中恰好发生一次。
在Python3:
import string
def latin8a(i, j):
return i ^ j
def latin8b(i, j):
b = i >> 2
return (i << 1) ^ (b << 3 | b << 1 | b) ^ j
a_teams = string.ascii_uppercase[:8]
b_teams = string.ascii_uppercase[8:16]
for i in range(8):
print()
print('Round', i + 1)
for j in range(6):
print(a_teams[latin8a(i, j)], 'vs', b_teams[latin8b(i, j)],
'in game type', string.ascii_lowercase[j])
输出:
Round 1
A vs I in game type a
B vs J in game type b
C vs K in game type c
D vs L in game type d
E vs M in game type e
F vs N in game type f
Round 2
B vs K in game type a
A vs L in game type b
D vs I in game type c
C vs J in game type d
F vs O in game type e
E vs P in game type f
Round 3
C vs M in game type a
D vs N in game type b
A vs O in game type c
B vs P in game type d
G vs I in game type e
H vs J in game type f
Round 4
D vs O in game type a
C vs P in game type b
B vs M in game type c
A vs N in game type d
H vs K in game type e
G vs L in game type f
Round 5
E vs L in game type a
F vs K in game type b
G vs J in game type c
H vs I in game type d
A vs P in game type e
B vs O in game type f
Round 6
F vs J in game type a
E vs I in game type b
H vs L in game type c
G vs K in game type d
B vs N in game type e
A vs M in game type f
Round 7
G vs P in game type a
H vs O in game type b
E vs N in game type c
F vs M in game type d
C vs L in game type e
D vs K in game type f
Round 8
H vs N in game type a
G vs M in game type b
F vs P in game type c
E vs O in game type d
D vs J in game type e
C vs I in game type f