# 用Python形象地解决酒缸分酒问题

### 3,Python 模拟

3种路径的示意图

```t=8
e=4
x,y=5,3
if y>x:
x,y=y,x #保证x>y
#从路径来看，一个点要么没有路径，要么有1条路径，要么2条，没有其他情况；
#0条对应：(0,0) & (x,y)
#1条：(x,0) & (0,y)
#2条：(n,m) 0<n<x   0<m<y
r={}
r[(0,0)]=[]
r[(x,y)]=[]
r[(x,0)]=[(x-y,y)]
r[(0,y)]=[(y,0)]
for n in range(1,x):
if n<y:
r[(n,0)]=[(0,n),(n,y)]
else:
r[(n,0)]=[(n-y,y),(n,y)]
k=n+y
if k>x:
r[(n,y)]=[(n,0),(x,n+y-x)]
else:
r[(n,y)]=[(n,0),(n+y,0)]
for m in range(1,y):
#m必然要小于x if m<x:  不需要
r[(0,m)]=[(m,0),(x,m)]
r[(x,m)]=[(0,m),(x+m-y,y)]```

```w=[x,0]  #上一个点
s=[x,0]
plst=[s] #开始
while s[0]!=e and s[1]!=e:
ss=(s[0],s[1])
sw=r[ss]
slen=len(sw)

if slen==1:
w=s.copy()
s[0]=sw[0][0]
s[1]=sw[0][1]
elif slen==2:
if sw[0][0]==w[0] and sw[0][1]==w[1]:
w=s.copy()
s[0]=sw[1][0]
s[1]=sw[1][1]
elif sw[1][0]==w[0] and sw[1][1]==w[1]:
w=s.copy()
s[0]=sw[0][0]
s[1]=sw[0][1]
else:
print(s,sw,slen)
else:
print(s,sw,slen)
plst.append(s)

print(plst)```

### 4,turtle可视化

```def drawToXY(x,y,turtle=turtle,sin=sin,pc=50): #sin=sin(60°)=sqrt(3)/2; pc转换因子
turtle.goto(x*pc+0.5*y*pc,pc*sin*y) #直角坐标转斜坐标```

```def drawDiamond(x,y,turtle=turtle,pc=pc,sin=sin):  #画边界
turtle.pensize(3) #占3个像素的笔宽
drawToXY(x,0,turtle,sin)
drawToXY(x,y,turtle,sin)
drawToXY(0,y,turtle,sin)
drawToXY(0,0,turtle,sin)  #外框
turtle.pensize(1) #默认笔宽```

```def drawPlst(plst,turtle=turtle,t2=t2):
dt=25
dy=200
k=0
for p in plst:
drawText(-180,dy-k*dt,str(p),t2,size=14) #在另一侧写下经过的边界点
drawToXY(p[0],p[1],t1)
k+=1

def drawText(x,y,txt,turtle=turtle,size=14):
turtle.penup()
turtle.goto(x,y)
turtle.pendown()
turtle.write(txt, font=("微软雅黑", size, "normal"))
plst=plst=[(5,0),(2,3),(2,0),(0,2),(5,2),(4,3)]
t1.color("red")
#t1.speed(2) #绘制速度，[0,10] 数值越大速度越快
drawPlst(plst,t1,t2)```

18 篇文章13 人订阅

0 条评论