我该如何用Javascript解决这个组合场景?

时间:2015-06-27 22:16:39

标签: javascript combinations lodash

对于至少有8支球队且最多为18支的锦标赛,我必须确定比赛日历。比赛有17轮或比赛日。因此每支球队每场比赛日必须遇到另一支球队。如果不到18支球队的遭遇可以重复,那么一支球队可以不止一次与另一支球队比赛。

This is an example for 18 teams tournament. And this would be a case for less than 18 teams fixture, here in particular 9 teams

所以,我必须进行排列,然后在不同的回合中安排它们。我试过了:

组合:

function k_combinations(set, k) {
    var i, j, combs, head, tailcombs;

    if (k > set.length || k <= 0) {
        return [];
    }

    if (k == set.length) {
        return [set];
    }

    if (k == 1) {
        combs = [];
        for (i = 0; i < set.length; i++) {
            combs.push([set[i]]);
        }
        return combs;
    }

    combs = [];
    for (i = 0; i < set.length - k + 1; i++) {
        head = set.slice(i, i+1);
        tailcombs = k_combinations(set.slice(i + 1), k - 1);
        for (j = 0; j < tailcombs.length; j++) {
            combs.push(head.concat(tailcombs[j]));
        }
    }
    return combs;
}

var teams = [   {name: 'Real Madrid'},
                {name: 'Las Palmas'},
                {name: 'Alavés'},
                {name: 'Valencia'},
                {name: 'Sevilla'},
                {name: 'Betis'},
                {name: 'Córdoba'},
                {name: 'Deportivo'},
                {name: 'Atlético de Madrid'},
                {name: 'Levante'},
                {name: 'Rayo Vallecano'},
                {name: 'Athletic Bilbao'},
                {name: 'Osasuna'},
                {name: 'Zaragoza'},
                {name: 'Villareal'},
                {name: 'Racing de Santander'},
                {name: 'Espanyol'},
                {name: 'Cádiz'},
                ];
// Compute whole encounters combinations.
var seasonMatches = k_combinations(teams,2);

组合的安排:

var calendar = {};
for (var i = 0; i<17; i++) {
    calendar[i+1] = [];
}
var encounters = seasonMatches;

for (var i = 0; i<Object.keys(calendar).length; i++) {

    encounters.map(function (match,index) {

        if (! _.any(calendar, function (m) {

           return m[0].name === match[0].name || m[1].name === match[1].name || m[0].name === match[1].name || m[1].name === match[0].name;
        })) {
            calendar[i+1].push(match);
        }
    });
}

我使用lodash来简化前一轮中任何遭遇的存在检查。

我遇到的问题是,这样我在日历中的每一轮都遇到了相同的遭遇。而且,如果我为seasonMatches添加一个拼接,我最终会得到每轮不同的匹配。

I've got a fiddle with this example shown above. 我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

看起来你喜欢努力工作:)有一种更简单的方法(jsbin link):

var teamsCount = 9;
var matchDays = 17;
var matches = [];
var teams = _.shuffle(_.range(teamsCount));
while(matches.length < matchDays){
  var newMatches = _(teams).chunk(2).partition(function(match){
    return match.length === 2;
  }).value();
  matches = matches.concat(newMatches[0]);
  if(newMatches[1].length) { // one team was left out, let's make sure it is playing
    // we put it first, and add the other teams, shuffled, without that one team
    teams = newMatches[1][0].concat(_.shuffle(_.without(_.range(teamsCount), newMatches[1][0][0])));
  } else {
    teams = _.shuffle(_.range(teamsCount));
  }
}

// we might get more then we need
matches = _.take(matches, matchDays);

_.each(matches, function(match, index){
  console.log('round ' + index + ': ' + match);
});

说明: 由于你没有施加任何其他限制(例如,每支球队必须互相比赛),所以只需要一支球队,将他们洗牌并一次打两局(=一场比赛)就足够了。然后我们将块分区为真正的匹配(2个队列阵)并留下(1个队列阵列)。

我们采用真实的匹配并将它们添加到现有的匹配中。如果我们已经离开了,我们会保留它,然后将洗牌后的球队(没有剩下的队员)连接起来,然后重新组合。我们继续,直到我们有足够的比赛。既然我们可能需要更多的匹配,我们只需要前17个。

JSbin更精细,并将匹配转换为团队名称。

我试着查看你的代码,看看为什么你得到你展示的模式,但这对我来说太复杂了,我喜欢用简单的方法做事; - )

相关问题