gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 00:13:22 26 4
gpt4 key购买 nike

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

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

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

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

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

$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。

最佳答案

这个问题可以在没有任何猜测或回溯的情况下解决,方法是为 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);

关于php - 创建行和列中唯一的数字矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15946255/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com