私は Python を知りませんが、Ruby で試してみることに抵抗できませんでした。うまくいけば、これはすぐに Python に変換されます。Ruby を知らない場合は、ここで何が起こっているのかを喜んで説明します。
num_players = gets.to_i
players = (1..num_players).to_a
teams = players.combination(2).to_a
def shuffle_teams( teams, players )
shuffled_teams = teams.shuffle
x = 0
while x < shuffled_teams.length
if shuffled_teams[x] - shuffled_teams[x + 1] == shuffled_teams[x]
x += 2
else
return shuffle_teams( teams, players )
end
end
x = 0
while x < shuffled_teams.length
team_1 = shuffled_teams[x]
team_2 = shuffled_teams[x + 1]
waiting = players.select do |player|
![team_1, team_2].flatten.include?(player)
end
print "(#{team_1}, #{team_2}), waiting: #{waiting}\n"
x += 2
end
end
shuffle_teams( teams, players )
これにより、4 人のプレーヤーの正しい出力が生成されます。
([3, 4], [1, 2]), waiting: []
([1, 3], [2, 4]), waiting: []
([2, 3], [1, 4]), waiting: []
5 人のプレーヤーの場合:
([2, 4], [1, 3]), waiting: [5]
([1, 5], [3, 4]), waiting: [2]
([1, 4], [2, 5]), waiting: [3]
([3, 5], [1, 2]), waiting: [4]
([2, 3], [4, 5]), waiting: [1]
ただし、6 人または 7 人でプレイすると、組み合わせが奇数になるため、うまくいきません。この問題は実際の生活の中でどのように扱われますか? どういうわけか、1 つのチームが 2 回プレイする必要があります。
編集:このスクリプトは、チームの 1 つを複製することにより、6 人または 7 人のプレイヤー プールを処理するようになりました。適切な順序に落ち着くまでチームの配列をシャッフルするだけなので、Python で複製するのは簡単なはずです。最初は、そのアプローチで少しごまかしているように感じましたが、これがNP完全問題であるというAnonymousの説明を考えると(それが何を意味するのかを正しく理解していると仮定して:-)、これが問題を解決する最良の方法かもしれません小さなプール (システムによっては、9 かそこらを超えるプールで爆発しますが、幸いなことに、それはシナリオの範囲を超えています)。さらに、ランダムな順序付けには非個人的であるという利点があり、プレーヤーが 2 回目のスコアを獲得せずに 2 回プレイしなければならないことに腹を立てている場合に役立ちます! スクリプトは次のとおりです。
num_players = gets.to_i
players = (1..num_players).to_a
teams = players.combination(2).to_a
def shuffle_teams( teams, players )
shuffled_teams = teams.shuffle
x = 0
while x < shuffled_teams.length
if !shuffled_teams[x + 1]
shuffled_teams[x + 1] = shuffled_teams.find do |team|
shuffled_teams[x] - team == shuffled_teams[x]
end
end
if shuffled_teams[x] - shuffled_teams[x + 1] == shuffled_teams[x]
x += 2
else
return shuffle_teams( teams, players )
end
end
x = 0
while x < shuffled_teams.length
team_1 = shuffled_teams[x]
team_2 = shuffled_teams[x + 1]
waiting = players.select do |player|
![team_1, team_2].flatten.include?(player)
end
print "(#{team_1}, #{team_2}), waiting: #{waiting}\n"
x += 2
end
end
shuffle_teams( teams, players )
そして、これが時間付きの出力です:
4
([1, 4], [2, 3]), waiting: []
([1, 2], [3, 4]), waiting: []
([2, 4], [1, 3]), waiting: []
real 0m0.293s
user 0m0.035s
sys 0m0.015s
5
([4, 5], [1, 2]), waiting: [3]
([1, 4], [2, 3]), waiting: [5]
([2, 5], [1, 3]), waiting: [4]
([2, 4], [3, 5]), waiting: [1]
([3, 4], [1, 5]), waiting: [2]
real 0m0.346s
user 0m0.040s
sys 0m0.010s
6
([3, 4], [1, 2]), waiting: [5, 6]
([3, 5], [2, 4]), waiting: [1, 6]
([3, 6], [1, 5]), waiting: [2, 4]
([1, 6], [2, 5]), waiting: [3, 4]
([2, 3], [4, 6]), waiting: [1, 5]
([2, 6], [4, 5]), waiting: [1, 3]
([5, 6], [1, 4]), waiting: [2, 3]
([1, 3], [2, 4]), waiting: [5, 6]
real 0m0.348s
user 0m0.035s
sys 0m0.020s
7
([1, 6], [4, 5]), waiting: [2, 3, 7]
([2, 6], [1, 4]), waiting: [3, 5, 7]
([2, 7], [1, 3]), waiting: [4, 5, 6]
([3, 4], [2, 5]), waiting: [1, 6, 7]
([3, 5], [2, 4]), waiting: [1, 6, 7]
([1, 7], [5, 6]), waiting: [2, 3, 4]
([6, 7], [1, 5]), waiting: [2, 3, 4]
([3, 6], [4, 7]), waiting: [1, 2, 5]
([1, 2], [5, 7]), waiting: [3, 4, 6]
([3, 7], [4, 6]), waiting: [1, 2, 5]
([2, 3], [1, 6]), waiting: [4, 5, 7]
real 0m0.332s
user 0m0.050s
sys 0m0.010s