1 回答

TA贡献1779条经验 获得超6个赞
制作集合列表,列表索引对应列,集合包含寻址列的索引。
扫描行时,添加链接列索引进行设置。最后检查设置长度。
您的情况示例(我使用列名而不是索引来避免错误):
sets[C1] = {C2}
sets[C5] = {C1,C3}
速成代码:
matrix = [[1,1,0,0,0],[0,0,1,0,0],[1,1,0,1,0],[0,0,1,0,1],[0,0,1,1,0]]
sets = []
for col in range(len(matrix[0])):
sets.append(set())
for row in range(len(matrix)):
leftone = -1
for col in range(len(matrix[row])):
if matrix[row][col] == 1:
if leftone < 0:
sets[col].add(col)
else:
sets[col].add(leftone)
leftone = col
for col in range(len(matrix[0])):
print(sets[col], len(sets[col]) == 1)
>>>
{0} True
{0} True
{2} True
{1, 2} False
{2} True
添加回答
举报