目前我实际上正在寻找一个专门针对我的问题的术语:
我创建了一个超过 4 支球队的联赛 联赛持续 3 轮(为了简单起见,编号) 比赛应从一支球队尚未交战的球队中随机分配。
我正在努力让我当前的代码在每个边缘情况下运行,所以我想查找为这种情况开发的“标准”算法,但我无法想出我正在寻找的术语。
一个时间表示例是:
TeamA: C,E,B
TeamB: F,H,A
TeamC: A,D,H
TeamD: G,C,F
TeamE: H,A,G
TeamF: B,G,D
TeamG: D,F,G
TeamH: E,B,C
在这方面我找不到任何东西,因为这似乎是一个非常非常不可能在联赛/锦标赛中使用的东西——但这是我的要求。
这是我当前创建 ONE Round 的代码。可能会发生,此代码不会在第 3 轮中为每个团队分配一个对手,因为他们可能的对手在本轮已经分配了一场比赛(测试了 6 支球队,可能在第 3 轮中发生)
public function CalculateDivision()
{
$teams = Division::find(1)->teams()->get();
$diffs = array();
foreach($teams as $team)
{
//Get possible Opponents
$opp = Division::find(1)->teams()->where('id','!=',$team->id)->lists('id');
$matches = $team->matches()->get();
$plyd = array();
foreach($matches as $match)
{
//Find Opponents a team already has played against
$plyd[] = $match->teams()->where('id','!=',$team->id)->pluck('id');
}
//Substract Opponents already played against from possible Opponents
$TBP = array_diff($opp,$plyd);
$diffs[$team->id] = $TBP;
}
//Order By Least possible Opponents possible
asort($diffs);
$this->CalculateMatches($diffs);
}
private function CalculateMatches($teams)
{
//$teams equals $teams[teamID] = [Opponent1ID,Opponent2ID ...]
$setTeams = array();
foreach($teams as $key => $team)
{
//If Team hasn't already a new matchup find opponent from their possible opponent array
if(!in_array($key,$setTeams))
{
shuffle($team);
foreach($team as $opponent)
{
//If possible opponent hasn't already a matchup create one, add both teams to 'has already a match' so the loop doesn't evaluate them again
if(!in_array($opponent,$setTeams))
{
$this->CreateMatch($key,$opponent);
$setTeams[] = $key;
$setTeams[] = $opponent;
break;
}
}
}
}
}
对我将谷歌搜索的任何帮助将不胜感激