2017-01-21 121 views
-2

有没有人知道我可以如何将一个数组拆分成两个组,而没有任何组(团队在这种情况下)再次会见对方,直到耗尽可能性?拆分数组,随机洗牌

阵列[0,1,2,3,4,5]

+0

请说明你已经做了什么来尝试解决这个问题。 –

回答

0

这绝对不是要做到这一点的最佳方式,请你自己优化它(这将仅偶数队的工作):

<?php 
    $allTeams = [0,1,2,3,4,5]; 
    $everyGames = [];//Append team matches to this 
    foreach($allTeams as $team1){ 
     foreach($allTeams as $team2){ 
      if (!in_array([$team2, $team1], $everyGames) 
       && $team2 !== $team1) { 
       $everyGames[] = [$team1, $team2]; 
      }    

     } 
    } 
    $copyOfEveryGames = $everyGames; 
    $needToLoopAgain = true; 
    while($needToLoopAgain){ 
     $allRounds = []; 
     $needToLoopAgain = false; 
     $everyGames = $copyOfEveryGames; 
     shuffle($everyGames);//Shuffle it for the random array 
     while(count($everyGames) > 0){ 
      $allMatches = []; 
      $tempEveryGames = $everyGames; 
      for($i = 0; $i < count($allTeams)/2; $i++){ 
       foreach($tempEveryGames as $key => $game){ 
        if(!(in_array_r($game[0], $allMatches) || in_array_r($game[1], $allMatches))){ 
         $allMatches[] = $game; 
         unset($tempEveryGames[$key]); 
         break; 
        } 
       } 
      } 
      if(count($allMatches) === count($allTeams)/2){ 
       $everyGames = $tempEveryGames ; 

       $allRounds[] = $allMatches; 
      }else{ 
       $needToLoopAgain = true; 
       break; 
      } 
     } 
     if(count($allRounds) !== count($allTeams) -1){ 
      $needToLoopAgain = true; 
     } 
    } 

    //Display Example 
    foreach($allRounds as $round){ 
     print_r($round); 
     echo "<br>"; 
    } 
    /* 
    Example Output: 
    Array ([0] => Array ([0] => 0 [1] => 3) 
      [1] => Array ([0] => 1 [1] => 4) 
      [2] => Array ([0] => 2 [1] => 5)) 
    Array ([0] => Array ([0] => 0 [1] => 2) 
      [1] => Array ([0] => 4 [1] => 5) 
      [2] => Array ([0] => 1 [1] => 3)) 
    Array ([0] => Array ([0] => 1 [1] => 2) 
      [1] => Array ([0] => 3 [1] => 4) 
      [2] => Array ([0] => 0 [1] => 5)) 
    Array ([0] => Array ([0] => 1 [1] => 5) 
      [1] => Array ([0] => 0 [1] => 4) 
      [2] => Array ([0] => 2 [1] => 3)) 
    Array ([0] => Array ([0] => 0 [1] => 1) 
      [1] => Array ([0] => 2 [1] => 4) 
      [2] => Array ([0] => 3 [1] => 5)) 
    */ 

    //Copied from http://stackoverflow.com/questions/4128323/in-array-and-multidimensional-array 
    function in_array_r($needle, $haystack, $strict = false) { 
     foreach ($haystack as $item) { 
      if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) { 
       return true; 
      } 
     } 

     return false; 
    } 

?> 
0

我希望这会回答你的问题,如果我很好地理解你的问题。

function againstWho(array $teams) : array{ 
    $fixture = null; 

    foreach($teams as $key => $team){ 

     unset($teams[$key]); 
     $fixture[$team] = $teams; 
     $teams[] = $team; 

    } 

    return $fixture; 
} 

// EXAMPLE 

$teams = ['MANU', 'ARS', 'LIV', 'CHE', 'MANC']; 

foreach (againstWho($teams) as $team => $fixtures){ 

    echo "$team will face "; 
    foreach ($fixtures as $key => $rival){ 
     echo "$rival, "; 
    } 
    echo "<br />"; 
} 

// Her's what gets printed 
/* 

MANU will face ARS, LIV, CHE, MANC, 
ARS will face LIV, CHE, MANC, MANU, 
LIV will face CHE, MANC, MANU, ARS, 
CHE will face MANC, MANU, ARS, LIV, 
MANC will face MANU, ARS, LIV, CHE 

*/