2

助けが必要です。たとえば、チーム名に 4 の配列を取得しました。

array('logiX.L4d22','Lust','Marat and Friends','Pandas of Belgium');

最初がラウンド、2 番目が試合、3 番目が互いにプレーするチームである 3 次元配列を作成したい場合、常に 2 つのチームが存在します。

ロジックは、すべてのチームが他のすべてのチームと対戦する必要があり、1 つのラウンドでどのチームも 1 つの試合のみを行うようにする必要があります。

次のようなものを生成する必要があります。

配列
  0 =>
    配列
      0 =>
        配列
          0 => 文字列 'logiX.L4D2' (長さ = 10)
          1 => 文字列 'Lust' (長さ = 4)
      1 =>
        配列
          0 => 文字列 'マラーと友達' (長さ = 17)
          1 => 文字列 'ベルギーのパンダ' (長さ = 17)
  1 =>
    配列
      0 =>
        配列
          0 => 文字列 'logiX.L4D2' (長さ = 10)
          1 => 文字列 'マラーと友達' (長さ = 17)
      1 =>
        配列
          0 => 文字列 'Lust' (長さ = 4)
          1 => 文字列 'ベルギーのパンダ' (長さ = 17)
  2 =>
    配列
      0 =>
        配列
          0 => 文字列 'logiX.L4D2' (長さ = 10)
          1 => 文字列 'ベルギーのパンダ' (長さ = 17)
      1 =>
        配列
          0 => 文字列 'Lust' (長さ = 4)
          1 => 文字列 'マラーと友達' (長さ = 17)

2,3,5 ...10 ...12 チームで作業する必要があります。

私はあなたが私を助けてくれることを願っています.私はすでにそのために1日半を費やしました.

4

2 に答える 2

2

ラウンド ロビン アルゴリズム PHPでいくつかのグーグル検索を行うと、次のようになります。

http://speedtech.it/blog/2009/03/15/round-robin-algorithm-php/

http://www.phpbuilder.com/board/showthread.php?t=10300945

お探しのものが見つかることを願っています。

編集

Wikipedia で説明されているラウンドロビンアルゴリズムに従って、これに私の試みを追加します。

チーム番号が奇数の場合、配列にチームが追加され (null 値)、ラウンドごとに「待機中のチーム」を取得できます。

<?php

$teams = range('a', 'g');

function make_rounds($teams)
{
  $nb_teams = count($teams);

  if ($nb_teams % 2 != 0)
  {
    $teams[] = null;
    $nb_teams++;
  }

  $nb_rounds = $nb_teams - 1;
  $nb_matches = $nb_teams / 2;

  $rounds = array();

  for($round_index = 0; $round_index < $nb_rounds; $round_index++)
  {
    $matches = array();

    for($match_index = 0; $match_index < $nb_matches; $match_index++)
    {
      if ($match_index == 0)
        $first_team = $teams[0];
      else
        $first_team = $teams[(($nb_teams-2) + $match_index - $round_index) % ($nb_teams-1) + 1];

      $second_team = $teams[(($nb_teams*2) - $match_index - $round_index - 3) % ($nb_teams-1) + 1];

      $matches[] = array($first_team, $second_team);
    }

    $rounds[] = $matches;
  }

  return $rounds;
}

print_r(make_rounds($teams));
于 2011-03-30T11:54:54.733 に答える
1

私のバージョンのソリューション。私はそれをブルートフォースと呼んでいます。しかし、それは何とか機能します。(または、そのように見えます。)

<?php

$a = array('a','b','c','d','e','f','g');

function do_array($a)
{
    $lim = sizeof($a) - 1;

    # Create an array of all matches to play.
    $cross = array(array());
    foreach (range(0,$lim) as $k_row):
        foreach (range(0,$lim) as $k_col):
            if ($k_row >= $k_col):
                $toput = false; 
            else:
                $toput = array($a[$k_row],$a[$k_col]);
            endif;
            $cross[$k_row][$k_col] = $toput;
        endforeach;
    endforeach;

    $ret = array();
    foreach (range(0,$lim) as $k_round):
        $round = array();
        # $tmp array holds all possible matches
        # to play in current round.
        $tmp = $cross;
        $i = 0;
        foreach (range(0,$lim) as $k_row):
            foreach (range(0,$lim) as $k_col):
                if ($math = $tmp[$k_row][$k_col]):
                    $cross[$k_row][$k_col] = false;
                    # These matches are not possible
                    # in the current round.
                    foreach (range(0,$lim) as $k):
                        $tmp[$k][$k_col] = false;
                        $tmp[$k][$k_row] = false;
                        $tmp[$k_col][$k] = false;
                        $tmp[$k_row][$k] = false;
                    endforeach;
                    $round[] = $math;
                endif;
            endforeach;
        endforeach;
        if ($round):
            $ret[] = $round;
        endif;
    endforeach;

    return $ret;
}

print_r (do_array($a));

?>
于 2011-03-30T13:19:59.597 に答える