|
发表于 2019-11-27 20:04:57
|
显示全部楼层
本楼为最佳答案
- import queue
- def possible(list3):
- list1 = list3[-1]
- result = []
- list2 = [8,5,3]
- temp1 = [True if list1[i] > 0 else False for i in range(3)]
- temp2 = [True if list1[i] != list2[i] else False for i in range(3)]
- for i in range(3):
- if temp1[i] == True:
- for j in range(3):
- if temp2[j] == True:
- if i != j:
- temp = list1.copy()
- if list2[j] - list1[j] >= list1[i]:
- temp[i] = 0
- temp[j] += list1[i]
- list4 = list3.copy()
- list4.append(temp)
- result.append(list4)
- else:
- temp[i] -= list2[j] - list1[j]
- temp[j] = list2[j]
- list4 = list3.copy()
- list4.append(temp)
- result.append(list4)
- return result
-
- list2 = [8,0,0]
- array = queue.Queue()
- array.put([list2])
- flag = False
- while not (array.empty() or flag == True):
- temp = array.get()
- temp2 = possible(temp)
- for each in temp2:
- if not 4 in each[-1]:
- array.put(each)
- else:
- print('解答')
- print(each)
- flag = True
- break
复制代码
不知道这个算不算广度优先。 |
|