七段码为什么用全排列

此时物理链接情况为 c -> g -> f ,但文字为 c -> f -> g

如果用组合”cfg“,显然 c 不在 dict[f] 中

所以必须用全排列满足这部分情况

如果用了组合算,结果是40,比正答少了一半

import os
import sys
import itertools

# 请在此输入您的代码
# 表示相邻元素
dict = {'a': ['f', 'b'], 'b': ['a', 'c', 'g'], 'c': ['b', 'd', 'g'],
        'd': ['e', 'c'], 'e': ['d', 'f', 'g'], 'f': ['a', 'e', 'g'],
        'g': ['b', 'c', 'e', 'f']}
s = 'abcdefg'
ans = []
cnt = 0
# 先找出全排列 在去全排列里筛选
for i in range(1, 8):
    for x in itertools.combinations(s, i):
        ans.append("".join(x))
print(ans)
print(len(ans))


for str1 in ans:# 每一种组合
    # 点亮一个的情况
    if len(str1) == 1:
        cnt += 1
        continue

    #由于"c"没有在dic["f"]中,所以就会遗落"cfg"这种情况
    # for c in range(1, len(str1)):   # 组合内遍历是否满足条件
    #     if str1[c - 1] not in dict[str1[c]]:
    #         flag = 0
    #         break
    #
    # else:
    #     cnt += 1
    #     print(str1)
    for str1 in ans:
        if len(str1) == 1:
            cnt += 1
            continue

        for situation in itertools.permutations(str1):
            for c in range(1, len(situation)):
                if situation[c - 1] not in dict[situation[c]]:
                    break

            else:
                cnt += 1
                break

print(cnt)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

汲海

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值