创建行和列中唯一的数字矩阵

2022-08-30 23:42:31

如果您在阅读问题后可以想出更好的标题,请随时更改它。

因此,作为输入,我有一个整数,它是一个介于 2 和 20 之间的偶数。我们称之为 整数 。我需要做的是生成一个介于 1 和(包括)之间的大小数字矩阵,同时遵守以下规则:$teams$teams x $teams$teams-1

  1. 对角线(从左上角到右下角)的值为 -1。
  2. 同一数字可能不会多次出现在同一列或同一行中。
  3. 如果数字出现在列 N 中,则 in 可能不会出现在 N 行中。例如,如果它出现在列 #2 中,则可能不会出现在第 2 行中,依此类推。

请注意,我们只查看对角线上方的部分。它下面的部分只是它的反映(每个数字都是它的反射+ $teams - 1),对于这个问题来说无关紧要。

前2个条件相当容易实现,但第3个条件正在杀死我。我不知道如何让它发生,特别是因为这个数字可以是2到20之间的任何偶数。下面给出了为条件 1 和 2 提供正确输出的代码。有人可以帮助我解决3号条件吗?$teams

$teams = 6;         //example value - should work for any even Int between 2 and 20
$games = array();   //2D array tracking which week teams will be playing

//do the work
for( $i=1; $i<=$teams; $i++ ) {
    $games[$i] = array();
    for( $j=1; $j<=$teams; $j++ ) {
        $games[$i][$j] = getWeek($i, $j, $teams);
    }
}

//show output
echo '<pre>';
$max=0;
foreach($games as $key => $row) {
    foreach($row as $k => $col) {
        printf('%4d', is_null($col) ? -2 : $col);
        if($col > $max){
            $max=$col;
        }
    }
    echo "\n";
}
printf("%d teams in %d weeks, %.2f weeks per team\n", $teams, $max, $max/$teams);
echo '</pre>';

function getWeek($home, $away, $num_teams) {
    if($home == $away){
        return -1;
    }
    $week = $home+$away-2;
    if($week >= $num_teams){
        $week = $week-$num_teams+1;
    }
    if($home>$away){
        $week += $num_teams-1;
    }

    return $week;
}

当前代码(表示 $teams=6)给出以下输出:

  -1   1   2   3   4   5
   6  -1   3   4   5   1
   7   8  -1   5   1   2
   8   9  10  -1   2   3
   9  10   6   7  -1   4
  10   6   7   8   9  -1
6 teams in 10 weeks, 1.67 weeks per team

如您所见,数字 1 同时出现在第 2 列和第 2 行中,数字 4 同时出现在第 5 列和第 5 行等中,这违反了规则 #3。


答案 1

通过为n支球队在n轮中相互比赛创建循环赛时间表,可以在没有任何猜测或回溯的情况下解决问题,然后从中构建一个数组,代表问题中描述的时间表

要构建时间表,请将n(此处为6)团队放在两行中

1 2 3
6 5 4

这是第 1 轮,其中 1 与 6 相遇,2 与 5 相遇,3 与 4 相遇。

然后,对于每轮,轮换除团队1以外的团队,给出完整的时间表

Round 1    Round 2    Round 3    Round 4    Round 5
1 2 3      1 3 4      1 4 5      1 5 6      1 6 2    
6 5 4      2 6 5      3 2 6      4 3 2      5 4 3  

这可以表示为一个数组,每行表示一周,其中第一列中的团队在最后一列与团队相遇,第二列与倒数第二方相遇,依此类推。

1 2 3 4 5 6  (Week 1: 1-6, 2-5, 3-4)
1 3 4 5 6 2  (Week 2: 1-2, 3-6, 4-5)
1 4 5 6 2 3  (Week 3: 1-3, 2-4, 5-6)
1 5 6 2 3 4  (Week 4: 1-4, 3-5, 2-6)
1 6 2 3 4 5  (Week 5: 1-5, 4-6, 2-3)

将团队表示为行和列,将周表示为表条目,这成为

-1  1  2  3  4  5
 6 -1  4  2  5  3
 7  9 -1  5  3  1
 8  7 10 -1  1  4
 9 10  8  6 -1  2
10  8  6  9  7 -1 

以下是为各种团队生成此代码的代码:

<?php

function buildSchedule($teams) {
  // Returns a table with one row for each round of the tournament                   
  // Matrix is built by rotating all entries except first one from row to row,       
  // giving a matrix with zeroes in first column, other values along diagonals       
  // In each round, team in first column meets team in last,                         
  // team in second column meets second last etc.                                    
  $schedule = array();
  for($i=1; $i<$teams; $i++){
    for($j=0; $j<$teams; $j++){
      $schedule[$i][$j] = $j==0 ? 0 : ($i+$j-1) % ($teams-1) + 1;
    }
  }
  return $schedule;
}

function buildWeekTable($schedule) {
  // Convert schedule into desired format                                            

  //create n x n array of -1                                                         
  $teams = sizeof($schedule)+1;
  $table = array_pad(array(), $teams, array_pad(array(), $teams, -1));

  // Set table[i][j] to week where team i will meet team j                           
  foreach($schedule as $week => $player){
    for($i = 0; $i < $teams/2 ; $i++){
      $team1 = $player[$i];
      $team2 = $player[$teams-$i-1];
      $table[$team1][$team2] = $team2 > $team1 ? $week : $week + $teams -1;
      $table[$team2][$team1] = $team1 > $team2 ? $week : $week + $teams -1;
    }
  }
  return $table;
}

function dumpTable($table){
  foreach($table as $row){
    $cols = sizeof($row);
    for($j=0; $j<$cols; $j++){
      printf(" %3d", isset($row[$j]) ? $row[$j] : -1);
    }
    echo "\n";
  }
}

$teams = 6;

$schedule = buildSchedule($teams);
$weekplan = buildWeekTable($schedule);
dumpTable($weekplan);

答案 2

我不相信有一种确定性的方法可以解决这个问题,除非你的程序做一些试错(猜测,然后回溯,如果猜测与规则冲突)。

我的想法是只修改getWeek()函数,但将数组传递给它,然后:$games

  1. 创建和数组所有矩阵元素,这些矩阵元素与我们的元素属于同一行或列
  2. 检查我们的一周是否已经属于同一行或相应的列
  3. 如果是这样,请使用我提供的公式随机选择它
  4. 在一段时间内循环执行此操作,直到猜测良好,然后继续前进

我为4,6,8,10和20个团队测试了它,它工作得很好。我设置了一个安全机制,该机制将设置为0,以防while循环变为无限循环,但这不会发生。$week

以下是完整的代码:

$teams = 10;
    $games = array();   //2D array tracking which week teams will be playing

    //do the work
    for( $i=1; $i<=$teams; $i++ ) {
        $games[$i] = array();
        for( $j=1; $j<=$teams; $j++ ) {
            $games[$i][$j] = getWeek($i, $j, $teams, $games);
        }
    }

    echo '<pre>';
    $max=0;
    foreach($games as $key => $row) {
        foreach($row as $k => $col) {
            printf('%4d', is_null($col) ? -2 : $col);
            if($col > $max){
                $max=$col;
            }
        }
        echo "\n";
    }
    printf("%d teams in %d weeks, %.2f weeks per team\n", $teams, $max, $max/$teams);
    echo '</pre>';

getWeek功能:

function getWeek($home, $away, $num_teams, $games) {
    if($home == $away){
        return -1;
    }
    $week = $home+$away-2;
    if($week >= $num_teams){
        $week = $week-$num_teams+1;
    }
    if($home>$away){
        $week += $num_teams-1;
    }

    $tries=0;
    $problems=array();

    //create array of all matrix elements that have the same row or column (regardless of value)
    foreach($games as $key => $row) {
        foreach($row as $k => $col) {
            if($home==$key || $home==$k || $away==$key || $away==$k)
                $problems[]=$col;   
        }
    }

    while(in_array($week, $problems)) {

        if($home<=$away)
                $week=rand(1,$num_teams-1);
            else
                $week=rand($num_teams,2*($num_teams-1));

            $tries++;
            if($tries==1000){
                $week=0;
                break;
            }
        }

    return $week;
}

这是的结果:$teams=10

  -1   1   2   3   4   5   6   7   8   9
  10  -1   3   4   5   6   7   8   9   2
  11  12  -1   5   6   7   8   9   1   4
  12  13  14  -1   7   8   9   1   2   6
  13  14  15  16  -1   9   1   2   3   8
  14  15  16  17  18  -1   2   3   4   1
  15  16  17  18  10  11  -1   4   5   3
  16  17  18  10  11  12  13  -1   6   5
  17  18  10  11  12  13  14  15  -1   7
  18  11  13  15  17  10  12  14  16  -1
10 teams in 18 weeks, 1.80 weeks per team

推荐