比赛支架放置算法 [英] Tournament bracket placement algorithm
问题描述
由于对手的种子列表(例如种子1到16),我试图写一个算法,将导致头号种子打在这一轮的最低种子,第二种子打的第二个最低的种子等等。
Given a list of opponent seeds (for example seeds 1 to 16), I'm trying to write an algorithm that will result in the top seed playing the lowest seed in that round, the 2nd seed playing the 2nd-lowest seed, etc.
分组1和16,2和15等为匹配是相当容易的,但我还需要确保更高的种子将发挥下种子在随后的几轮。
Grouping 1 and 16, 2 and 15, etc. into "matches" is fairly easy, but I also need to make sure that the higher seed will play the lower seed in subsequent rounds.
与正确放置一个例子支架:
An example bracket with the correct placement:
1 vs 16
1 vs 8
8 vs 9
1 vs 4
4 vs 13
4 vs 5
5 vs 12
1 vs 2
2 vs 15
2 vs 7
7 vs 10
2 vs 3
3 vs 14
3 vs 6
6 vs 11
正如你所看到的,种子1和2只满足在最后。
As you can see, seed 1 and 2 only meet up in the final.
推荐答案
我想出以下算法。它可能不是超高效的,但我不认为它真正需要的。它是用PHP编写的。
I've come up with the following algorithm. It may not be super-efficient, but I don't think that it really needs to be. It's written in PHP.
<?php
$players = range(1, 32);
$count = count($players);
// Order players.
for ($i = 0; $i < log($count / 2, 2); $i++) {
$out = array();
foreach ($players as $player) {
$splice = pow(2, $i);
$out = array_merge($out, array_splice($players, 0, $splice));
$out = array_merge($out, array_splice($players, -$splice));
}
$players = $out;
}
// Print match list.
for ($i = 0; $i < $count; $i++) {
printf('%s vs %s<br />%s', $players[$i], $players[++$i], PHP_EOL);
}
?>
这篇关于比赛支架放置算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!