You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

寻求每局3队及以上的联赛赛事调度程序开发方案

多队伍参赛的联赛赛程生成方案

问题分析

你需要生成n支队伍、每场k队参赛的联赛赛程,核心要求如下:

  • 每个k队组合恰好作为一场比赛出现(总比赛场数为组合数C(n,k))
  • 每个比赛日的所有比赛无重复队伍(当日所有队伍都参赛,分成n/k组)
  • 总比赛日数为组合数C(n-1,k-1)(每支队伍需与其他n-1队组成C(n-1,k-1)个k队组合,每个比赛日每队仅赛一场)

贪心实现方案(Python)

以下是基于贪心策略的Python实现,优先选择未安排的比赛,再填充同比赛日的其他无重复队伍的比赛,适用于n是k的倍数且满足组合数匹配的场景(如你提到的n=9,k=3):

import itertools

def generate_multi_team_schedule(n, k):
    # 生成所有可能的k队比赛组合
    all_games = list(itertools.combinations(range(n), k))
    # 用集合快速判断队伍是否重复
    game_to_teams = {game: frozenset(game) for game in all_games}
    used_games = set()
    schedule = []
    
    total_days = len(list(itertools.combinations(range(n-1), k-1)))
    
    while len(schedule) < total_days:
        # 选取第一个未安排的比赛作为当日起始
        start_game = None
        for game in all_games:
            if game not in used_games:
                start_game = game
                break
        if not start_game:
            break
        
        current_day = [start_game]
        used_games.add(start_game)
        used_teams = set(start_game)
        
        # 填充当日剩余比赛,确保无重复队伍
        while len(used_teams) < n:
            found = False
            for game in all_games:
                if game not in used_games and game_to_teams[game].isdisjoint(used_teams):
                    current_day.append(game)
                    used_games.add(game)
                    used_teams.update(game)
                    found = True
                    break
            # 若贪心填充失败,重置当前日重新选择起始比赛
            if not found:
                used_games.remove(start_game)
                current_day = []
                used_teams = set()
                break
        
        if current_day:
            schedule.append(current_day)
    
    return schedule

# 测试n=9,k=3的情况
if __name__ == "__main__":
    schedule = generate_multi_team_schedule(9, 3)
    for day_num, day_games in enumerate(schedule, 1):
        game_strs = [str(game) for game in day_games]
        print(f"第{day_num}比赛日: {', '.join(game_strs)}")

说明

  1. 贪心策略:每次从剩余比赛中选一场作为起始,再依次添加无重复队伍的比赛,直到填满当日所有队伍。若中途无法填充,则重置当前日重新选择起始,确保最终生成完整赛程。
  2. 适用场景:对于n和k满足C(n,k) % (n/k) == 0的情况(即总比赛场数能被每日比赛场数整除),该算法能稳定生成有效赛程。
  3. 性能:小n值(如n=9)瞬间完成;大n值可能耗时较长,但符合你“耗时数日可接受”的要求。若需进一步优化,可引入回溯算法或基于组合设计的构造方法(如有限域构造)。

内容的提问来源于stack exchange,提问作者Unnamed1242

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 15:33:13