题目
试题 D: 七段码
本题总分:10 分
【问题描述】
小蓝要用七段码数码管来表示一种特殊的文字。
上图给出了七段码数码管的一个图示,数码管中一共有 7 段可以发光的二极管,分别标记为 a, b, c, d, e, f, g。
小蓝要选择一部分二极管(至少要有一个)发光来表达字符。
在设计字符的表达时,要求所有发光的二极管是连成一片的。
例如:b 发光,其他二极管不发光可以用来表达一种字符。
例如:c 发光,其他二极管不发光可以用来表达一种字符。
这种方案与上一行的方案可以用来表示不同的字符,尽管看上去比较相似。
例如:a, b, c, d, e 发光,f, g 不发光可以用来表达一种字符。
例如:b, f 发光,其他二极管不发光则不能用来表达一种字符,因为发光的二极管没有连成一片。
请问,小蓝可以用七段码数码管表达多少种不同的字符?
【答案提交】
这是一道结果填空的题,你只需要算出结果后提交即可。
本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分
解题思路
参考别人的 还没搞明白
代码
#七段码
from itertools import combinations
#并查集模板
class UnionFindSet:
def __init__(self, n: int):
self.count = n
self.father, self.size = [i for i in range(n)], [1] * n
def find(self, x: int):
root = x
while self.father[root] != root:
root = self.father[root]
while x != root:
self.father[x], x = root, self.father[x]
return root
def same(self, x: int, y: int):
return self.find(x) == self.find(y)
def union(self, x: int, y: int):
rootX, rootY = self.find(x), self.find(y)
if rootX != rootY:
xSize, ySize = self.size[x], self.size[y]
rootX, rootY = (rootY, rootX) if xSize < ySize else (rootX, rootY)
self.father[rootY] = rootX
self.size[rootX] += self.size[rootY]
self.count -= 1
return False if rootX == rootY else True
ans, chs = 0, [0, 1, 2, 3, 4, 5, 6]
for i in range(1, len(chs) + 1):
for item in (combinations(chs, i)):
n = len(item)
ufs = UnionFindSet(n)
for j in range(n):
for k in range(j + 1, n):
if abs(item[j] - item[k]) == 1 or \
(item[j] == 0 and item[k] == 5) or \
(item[j] == 1 and item[k] == 6) or \
(item[j] == 2 and item[k] == 6) or \
(item[j] == 4 and item[k] == 6):
ufs.union(j, k)
ans += 1 if ufs.count == 1 else 0
print(ans)
结果
80