[Python] 纯文本查看 复制代码#获取某个位置之后3个的火柴
def Get3Plu(aDict: dict, index: int) -> int:
count = 0
for i in range(index + 1, 11):
if i in aDict.keys():
count += 1
if count == 3:
return i
return 0
#获取某个位置之前3个的火柴
def Get3Min(aDict: dict, index: int) -> int:
count = 0
for i in range(index - 1, 0, -1):
if i in aDict.keys():
count += 1
if count == 3:
return i
return 0
#获取所有移动的可能性
def GetAllPosible(aDict: dict) -> list:
result = []
for i in aDict.keys():
get3Plu = Get3Plu(aDict, i)
get3Min = Get3Min(aDict, i)
if get3Min != 0:
if not aDict[get3Min]:
result.append((i, get3Min))
if get3Plu != 0:
if not aDict[get3Plu]:
result.append((i, get3Plu))
return result
#处理一种可能性,火柴从opt[0]移动到opt[1]
def doStep(aDict: dict, opt: (int, int)):
newDict = aDict.copy()
newDict[opt[1]] = True
newDict.pop(opt[0])
return newDict
#做一大步处理, 由一种现状, 推测出所有可能性, 并返回所有处理结果
def goStep(aDict: dict):
all_result = []
temp_opt = GetAllPosible(aDict)
if temp_opt.__len__() > 0:
for opt in temp_opt:
temp_dic = doStep(aDict, opt)
all_result.append(temp_dic)
return all_result
if __name__ == '__main__':
list_All = []
# 数字表示位置, False为1根, True为2根, 没有(pop)为0根
orgin = {1: False, 2: False, 3: False, 4: False, 5: False, 6: False, 7: False, 8: False, 9: False, 10: False}
# 一定是5步 才OK
# 第一步
list_step = goStep(orgin)
# 剩下的4步
for i in range(4):
list_step_temp = []
for dic in list_step:
list_step_temp.extend(goStep(dic))
list_step = list_step_temp
print("共遍历了", len(list_step), "种方法")
result_list = []
for item in list_step:
if list(item.values()) == [True, True, True, True, True]:
result_list.append(item)
print(len(result_list), "种方法成功了")