1

我面临调度问题,

我们有 9 支球队..我们需要为每支球队安排一场与其他球队的比赛..

所以我们每周有 4 场比赛,持续 9 周。

现在使用下面的函数,我们可以得到所有匹配的组合 9c2

$matches = getCombinations(array(1, 2, 3, 4, 5, 6, 7, 8, 9), 2);

function getCombinations($teams, $group_length) {
    $teams_len = count($teams);
    if($group_length == 1) {
        $return = array();
        foreach($teams as $b){
            $return[] = array($b);
        }
        return $return;
    } else {
        //get one level lower combinations
        $oneLevelLower = getCombinations($teams, $group_length-1);

        //for every one level lower combinations add one element to them that the last element of a combination is preceeded by the element which follows it in base array if there is none, does not add
        $newCombs = array();

        foreach($oneLevelLower as $oll){

            $lastEl = $oll[$group_length-2];
            $found = false;
            foreach($teams as  $key => $b){
                if($b == $lastEl){
                    $found = true;
                    continue;
                    //last element found
                }
                if($found == true){
                    //add to combinations with last element
                    if($key < $teams_len){

                        $tmp = $oll;
                        $newCombination = array_slice($tmp,0);
                        $newCombination[]=$b;
                        $newCombs[] = array_slice($newCombination,0);
                    }
                }
            }
        }
    }
    return $newCombs;
}

返回所有匹配项..

array (size=36)
  0 => 
    array (size=2)
      0 => int 1
      1 => int 2
  1 => 
    array (size=2)
      0 => int 1
      1 => int 3
  2 => 
    array (size=2)
      0 => int 1
      1 => int 4
  3 => 
    array (size=2)
      0 => int 1
      1 => int 5
  4 => 
    array (size=2)
      0 => int 1
      1 => int 6
  5 => 
    array (size=2)
      0 => int 1
      1 => int 7
  6 => 
    array (size=2)
      0 => int 1
      1 => int 8
  7 => 
    array (size=2)
      0 => int 1
      1 => int 9
  8 => 
    array (size=2)
      0 => int 2
      1 => int 3
  9 => 
    array (size=2)
      0 => int 2
      1 => int 4
  10 => 
    array (size=2)
      0 => int 2
      1 => int 5
  11 => 
    array (size=2)
      0 => int 2
      1 => int 6
  12 => 
    array (size=2)
      0 => int 2
      1 => int 7
  13 => 
    array (size=2)
      0 => int 2
      1 => int 8
  14 => 
    array (size=2)
      0 => int 2
      1 => int 9
  15 => 
    array (size=2)
      0 => int 3
      1 => int 4
  16 => 
    array (size=2)
      0 => int 3
      1 => int 5
  17 => 
    array (size=2)
      0 => int 3
      1 => int 6
  18 => 
    array (size=2)
      0 => int 3
      1 => int 7
  19 => 
    array (size=2)
      0 => int 3
      1 => int 8
  20 => 
    array (size=2)
      0 => int 3
      1 => int 9
  21 => 
    array (size=2)
      0 => int 4
      1 => int 5
  22 => 
    array (size=2)
      0 => int 4
      1 => int 6
  23 => 
    array (size=2)
      0 => int 4
      1 => int 7
  24 => 
    array (size=2)
      0 => int 4
      1 => int 8
  25 => 
    array (size=2)
      0 => int 4
      1 => int 9
  26 => 
    array (size=2)
      0 => int 5
      1 => int 6
  27 => 
    array (size=2)
      0 => int 5
      1 => int 7
  28 => 
    array (size=2)
      0 => int 5
      1 => int 8
  29 => 
    array (size=2)
      0 => int 5
      1 => int 9
  30 => 
    array (size=2)
      0 => int 6
      1 => int 7
  31 => 
    array (size=2)
      0 => int 6
      1 => int 8
  32 => 
    array (size=2)
      0 => int 6
      1 => int 9
  33 => 
    array (size=2)
      0 => int 7
      1 => int 8
  34 => 
    array (size=2)
      0 => int 7
      1 => int 9
  35 => 
    array (size=2)
      0 => int 8
      1 => int 9

但是一支球队一天不可能有两场比赛,每支球队都需要四场比赛。

所以我们必须将这些比赛安排到每周的日程表中,这样每周有 8 支球队打 4 场比赛,剩下一支球队……每支球队有四场比赛……

*PS 我没有实现组合功能,它是从某人复制的..thnx 给他..我们使用 php 虽然我不认为语言在这里很重要..

4

1 回答 1

1

You can use a round-robin tournament scheduling algorithm to solve this, with nine regular teams and one "bye" team. Here is a PHP implementation.

于 2013-05-08T14:47:25.987 回答