我有清单:
words = ["ALI", "SIN", "ASI", "LIR", "IRI", "INI", "KAR"]
我想检查它们是否形成这样的矩阵:
并将我的解决方案作为列表返回,例如:
solution = ["ALI", "SIN", "IRI"]
我想出了这段代码:
words=["ALI", "SIN", "ASI", "LIR", "IRI", "INI", "KAR"]
solution =[]
failedsolutions = []
def Get_next_word():
while True:
for word in words:
if (word in solution) == False:
solution.append(word)
if (solution in failedsolutions) == False:
return False
else:
solution.pop(len(solution) - 1 )
return True
def Check_word_order():
checker = True
for i in range(len(solution)):
for j in range(len(words[0])):
for word in words:
if solution[i][j] == word[j]:
checker = False
else:
checker = True
if checker == False:
return checker
def main():
while True:
Get_next_word()
check = Check_word_order()
if check is False:
#Backtrack
failedsolutions.append(solution)
solution.pop(len(solution) - 1 )
# else:
# solution.append()
print(solution)
main()
我累了,不能再调试我的代码了。帮助将不胜感激。谢谢 ps:如果有更好的方法,我建议不要修复我的代码。