卡牌游戏设计:8个位置中R、B、G指定数量分布枚举需求
生成符合条件的卡牌分布列表
问题核心
需要生成所有满足以下条件的8位序列:
- 仅包含R、B、G三种元素
- 其中两种元素各出现3次,第三种出现2次
分类枚举思路
这类序列可分为3种互斥场景,分别计算每种场景的所有排列后合并即可:
- R出现2次,B和G各出现3次
- B出现2次,R和G各出现3次
- G出现2次,R和B各出现3次
每种场景的排列数计算方式:
以第一种场景为例,先从8个位置中选2个放置R,再从剩余6个位置中选3个放置B,最后3个位置自动填充G。组合数为:C(8,2) * C(6,3) = 28 * 20 = 560
三种场景总计 560 * 3 = 1680 种不同序列。
代码实现示例(Python)
如果需要批量生成所有序列,可使用以下代码:
import itertools def generate_sequences(): sequences = [] # 场景1: R=2, B=3, G=3 for r_pos in itertools.combinations(range(8), 2): remaining = [i for i in range(8) if i not in r_pos] for b_pos in itertools.combinations(remaining, 3): seq = [''] * 8 for pos in r_pos: seq[pos] = 'R' for pos in b_pos: seq[pos] = 'B' for pos in remaining: if pos not in b_pos: seq[pos] = 'G' sequences.append(''.join(seq)) # 场景2: B=2, R=3, G=3 for b_pos in itertools.combinations(range(8), 2): remaining = [i for i in range(8) if i not in b_pos] for r_pos in itertools.combinations(remaining, 3): seq = [''] * 8 for pos in b_pos: seq[pos] = 'B' for pos in r_pos: seq[pos] = 'R' for pos in remaining: if pos not in r_pos: seq[pos] = 'G' sequences.append(''.join(seq)) # 场景3: G=2, R=3, B=3 for g_pos in itertools.combinations(range(8), 2): remaining = [i for i in range(8) if i not in g_pos] for r_pos in itertools.combinations(remaining, 3): seq = [''] * 8 for pos in g_pos: seq[pos] = 'G' for pos in r_pos: seq[pos] = 'R' for pos in remaining: if pos not in r_pos: seq[pos] = 'B' sequences.append(''.join(seq)) return sequences # 生成并打印前10个示例 all_seqs = generate_sequences() print("示例序列:") for seq in all_seqs[:10]: print(seq)
部分示例序列
- RRBBBGGG
- RBRBBGGG
- RBBRBGGG
- RBBRGBGG
- RBBGRBGG
- RBBGGBRG
- RBBGGGRB
- BRRBBGGG
- BRBRBGGG
- BRBBRGGG
内容的提问来源于stack exchange,提问作者Timothy Hyer-Devine
相关产品推荐
相关产品推荐

