python ---倒酒!!
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import numbers
import numpy
import math
'''
三個容器分別為12升、8升、5升,初始時12升容器盛滿酒,就只能在三個容器中來回傾倒,如某容器中恰好6升就,則結束
3*2維陣列:三個二維陣列表示三個容器;二維陣列第一個數字表示容器大小,第二數字表示當前容器中的酒量
結束條件:前兩個二維陣列中第二個數字為6
倒酒的動作:兩個陣列A,B之間,第一個向第二個倒酒
B[1]=B[0]或A[1]=0不操作
若A[1]+B[1]>=B[0],則A[1]=A[1]+B[1]-B[0],B[1]=B[0]若B[0]>A[1]+B[1],則B[1]=A[1]+B[1],A[1]=0
到酒後的動作:記錄三個容器中的酒量到佇列:若任意一個等於6則列印返回;
若佇列中與當結果不存在重複,則繼續下一個倒酒動作
每個狀態有六種倒酒操作:1-》2 ,1-》3,2-》3,2-》1,3-》1,3->2
'''
#initstatus=[[12,2],[8,5],[5,5]]
class purewine(object):
def __init__(self):
pass
def purewine(self,A,B,a,b):if a + b >= B:
a = a + b - B
b = B
else:
b = a + b
a = 0
return A,B,a,b
def judge(self,A,B,C,a,b,c,t='',r=0):
restr=str(a)+str(b)+str(c)
if a==r or b==r or c==r:
t=t+','+restr
print(t)
return Trueelse:
if t.count(restr)<1:
t = t + ',' + restr
self.dynamictree(A,B,C,a,b,c,t,r)
def dynamictree(self,A,B,C,a,b,c,t='',r=0):
j,k,m,n=self.purewine(A,B,a,b,)
self.judge(j,k,C,m,n,c,t,r)
j,k,m,n = self.purewine(A, C, a, c)
self.judge(j, B, k, m, b, n, t, r)
j, k, m, n = self.purewine(B, C, b, c)
self.judge(A, j, k, a, m, n, t, r)
j, k, m, n = self.purewine(C, B, c, b)
self.judge(A, k, j, a, n, m, t, r)
j, k, m, n = self.purewine(C, A, c, a)
self.judge(k, B, j, n, b, m, t, r)
j, k, m, n = self.purewine(B, A, b, a)
self.judge(k, j, C, n, m, c, t, r)
wins=purewine()
wins.dynamictree(12,8,5,12,0,0,'1200',6)
輸出:
1200,480,084,804,840,345,381,1101,1110,615
1200,480,435,075,084,804,840,345,381,1101,1110,615
1200,480,435,075,570,525,1020,1002,282,084,804,840,345,381,1101,1110,615
1200,480,435,075,570,525,1020,1002,705,750,255,282,084,804,840,345,381,1101,1110,615
1200,480,435,075,570,525,705,750,255,282,084,804,840,345,381,1101,1110,615
1200,480,435,075,705,750,255,282,084,804,840,345,381,1101,1110,615
1200,480,435,930,903,183,084,804,840,345,381,1101,1110,615
1200,480,435,930,903,183,165
1200,480,435,930,903,705,075,084,804,840,345,381,1101,1110,615
1200,480,435,930,903,705,075,570,525,1020,1002,282,084,804,840,345,381,1101,1110,615
1200,480,435,930,903,705,750,255,075,084,804,840,345,381,1101,1110,615
1200,480,435,930,903,705,750,255,075,570,525,1020,1002,282,084,804,840,345,381,1101,1110,615
1200,480,435,930,903,705,750,255,282,084,804,840,345,381,1101,1110,615
1200,480,435,930,903,705,750,255,282,1002,1020,525,075,084,804,840,345,381,1101,1110,615
1200,480,435,930,903,705,750,255,282,1002,1020,525,570,075,084,804,840,345,381,1101,1110,615
1200,480,435,705,075,084,804,840,345,381,1101,1110,615
1200,480,435,705,075,570,525,1020,1002,282,084,804,840,345,381,1101,1110,615
1200,480,435,705,750,255,075,084,804,840,345,381,1101,1110,615
1200,480,435,705,750,255,075,570,525,1020,1002,282,084,804,840,345,381,1101,1110,615
1200,480,435,705,750,255,282,084,804,840,345,381,1101,1110,615
1200,480,435,705,750,255,282,1002,1020,525,075,084,804,840,345,381,1101,1110,615
1200,480,435,705,750,255,282,1002,1020,525,570,075,084,804,840,345,381,1101,1110,615
1200,705,075,084,480,435,930,903,183,165
1200,705,075,084,804,840,480,435,930,903,183,165
1200,705,075,084,804,840,345,381,480,435,930,903,183,165
1200,705,075,084,804,840,345,381,1101,1110,480,435,930,903,183,165
1200,705,075,084,804,840,345,381,1101,1110,615
1200,705,075,570,480,084,804,840,345,381,1101,1110,615
1200,705,075,570,480,435,930,903,183,084,804,840,345,381,1101,1110,615
1200,705,075,570,480,435,930,903,183,165
1200,705,075,570,525,1020,480,084,804,840,345,381,1101,1110,615
1200,705,075,570,525,1020,480,435,930,903,183,084,804,840,345,381,1101,1110,615
1200,705,075,570,525,1020,480,435,930,903,183,165
1200,705,075,570,525,1020,1002,282,084,480,435,930,903,183,165
1200,705,075,570,525,1020,1002,282,084,804,840,480,435,930,903,183,165
1200,705,075,570,525,1020,1002,282,084,804,840,345,381,480,435,930,903,183,165
1200,705,075,570,525,1020,1002,282,084,804,840,345,381,1101,1110,480,435,930,903,183,165
1200,705,075,570,525,1020,1002,282,084,804,840,345,381,1101,1110,615
1200,705,075,570,525,1020,1002,282,255,750,480,084,804,840,345,381,1101,1110,615
1200,705,075,570,525,1020,1002,282,255,750,480,435,930,903,183,084,804,840,345,381,1101,1110,615
1200,705,075,570,525,1020,1002,282,255,750,480,435,930,903,183,165
1200,705,075,570,525,1020,1002,282,480,084,804,840,345,381,1101,1110,615
1200,705,075,570,525,1020,1002,282,480,435,930,903,183,084,804,840,345,381,1101,1110,615
1200,705,075,570,525,1020,1002,282,480,435,930,903,183,165
1200,705,750,480,084,804,840,345,381,1101,1110,615
1200,705,750,480,435,075,084,804,840,345,381,1101,1110,615
1200,705,750,480,435,075,570,525,1020,1002,282,084,804,840,345,381,1101,1110,615
1200,705,750,480,435,930,903,183,084,804,840,345,381,1101,1110,615
1200,705,750,480,435,930,903,183,165
1200,705,750,255,075,084,480,435,930,903,183,165
1200,705,750,255,075,084,804,840,480,435,930,903,183,165
1200,705,750,255,075,084,804,840,345,381,480,435,930,903,183,165
1200,705,750,255,075,084,804,840,345,381,1101,1110,480,435,930,903,183,165
1200,705,750,255,075,084,804,840,345,381,1101,1110,615
1200,705,750,255,075,570,480,084,804,840,345,381,1101,1110,615
1200,705,750,255,075,570,480,435,930,903,183,084,804,840,345,381,1101,1110,615
1200,705,750,255,075,570,480,435,930,903,183,165
1200,705,750,255,075,570,525,1020,480,084,804,840,345,381,1101,1110,615
1200,705,750,255,075,570,525,1020,480,435,930,903,183,084,804,840,345,381,1101,1110,615
1200,705,750,255,075,570,525,1020,480,435,930,903,183,165
1200,705,750,255,075,570,525,1020,1002,282,084,480,435,930,903,183,165
1200,705,750,255,075,570,525,1020,1002,282,084,804,840,480,435,930,903,183,165
1200,705,750,255,075,570,525,1020,1002,282,084,804,840,345,381,480,435,930,903,183,165
1200,705,750,255,075,570,525,1020,1002,282,084,804,840,345,381,1101,1110,480,435,930,903,183,165
1200,705,750,255,075,570,525,1020,1002,282,084,804,840,345,381,1101,1110,615
1200,705,750,255,075,570,525,1020,1002,282,480,084,804,840,345,381,1101,1110,615
1200,705,750,255,075,570,525,1020,1002,282,480,435,930,903,183,084,804,840,345,381,1101,1110,615
1200,705,750,255,075,570,525,1020,1002,282,480,435,930,903,183,165
1200,705,750,255,282,084,075,570,480,435,930,903,183,165
1200,705,750,255,282,084,075,570,525,1020,480,435,930,903,183,165
1200,705,750,255,282,084,480,435,930,903,183,165
1200,705,750,255,282,084,804,840,480,435,930,903,183,165
1200,705,750,255,282,084,804,840,345,075,570,480,435,930,903,183,165
1200,705,750,255,282,084,804,840,345,075,570,525,1020,480,435,930,903,183,165
1200,705,750,255,282,084,804,840,345,381,480,435,930,903,183,165
1200,705,750,255,282,084,804,840,345,381,1101,1110,480,435,930,903,183,165
1200,705,750,255,282,084,804,840,345,381,1101,1110,615
1200,705,750,255,282,480,084,804,840,345,381,1101,1110,615
1200,705,750,255,282,480,435,075,084,804,840,345,381,1101,1110,615
1200,705,750,255,282,480,435,930,903,183,084,804,840,345,381,1101,1110,615
1200,705,750,255,282,480,435,930,903,183,165
1200,705,750,255,282,1002,1020,480,084,804,840,345,381,1101,1110,615
1200,705,750,255,282,1002,1020,480,435,075,084,804,840,345,381,1101,1110,615
1200,705,750,255,282,1002,1020,480,435,930,903,183,084,804,840,345,381,1101,1110,615
1200,705,750,255,282,1002,1020,480,435,930,903,183,165
1200,705,750,255,282,1002,1020,525,075,084,480,435,930,903,183,165
1200,705,750,255,282,1002,1020,525,075,084,804,840,480,435,930,903,183,165
1200,705,750,255,282,1002,1020,525,075,084,804,840,345,381,480,435,930,903,183,165
1200,705,750,255,282,1002,1020,525,075,084,804,840,345,381,1101,1110,480,435,930,903,183,165
1200,705,750,255,282,1002,1020,525,075,084,804,840,345,381,1101,1110,615
1200,705,750,255,282,1002,1020,525,075,570,480,084,804,840,345,381,1101,1110,615
1200,705,750,255,282,1002,1020,525,075,570,480,435,930,903,183,084,804,840,345,381,1101,1110,615
1200,705,750,255,282,1002,1020,525,075,570,480,435,930,903,183,165
1200,705,750,255,282,1002,1020,525,570,480,084,804,840,345,381,1101,1110,615
1200,705,750,255,282,1002,1020,525,570,480,435,075,084,804,840,345,381,1101,1110,615
1200,705,750,255,282,1002,1020,525,570,480,435,930,903,183,084,804,840,345,381,1101,1110,615
1200,705,750,255,282,1002,1020,525,570,480,435,930,903,183,165
1200,705,750,255,282,1002,1020,525,570,075,084,480,435,930,903,183,165
1200,705,750,255,282,1002,1020,525,570,075,084,804,840,480,435,930,903,183,165
1200,705,750,255,282,1002,1020,525,570,075,084,804,840,345,381,480,435,930,903,183,165
1200,705,750,255,282,1002,1020,525,570,075,084,804,840,345,381,1101,1110,480,435,930,903,183,165
1200,705,750,255,282,1002,1020,525,570,075,084,804,840,345,381,1101,1110,615