1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| # usr/bin/env python
# -*- coding: Latin-1 -*-
## Version 1 ##
def niveaup(nn,np,Urne,De,Vers):
global sol
for x in xrange(3):
for y in xrange(3):
if x!=y and Urne[x]>=np:
De[np],Vers[np]=x,y
Urne[x]-=np
Urne[y]+=np
if np==nn and Urne==[nn,nn,nn]:
sol+=1
print " ---> Solution n°",sol,"<---"
Affiche_resultats(nn,De,Vers)
if np<nn:
niveaup(nn,np+1,Urne,De,Vers)
Urne[x]+=np
Urne[y]-=np
return
def Affiche_resultats(nn,De,Vers):
U=[nn*3,0,0]
for i in xrange(1,nn+1):
U[De[i]]-=i
U[Vers[i]]+=i
print "p =",i,"de ",'ABC'[De[i]],"vers ",'ABC'[Vers[i]],\
" * en A : %2i " % U[0],"* en B : %2i " % U[1],"* en C : %2i " % U[2],"*"
print
# Programme principal
global sol
nmaxi,sol=5,0
De,Vers=[0]*(nmaxi+1),[0]*(nmaxi+1)
print " ##########################"
print " # Début du calcul #"
print " ##########################"
print
nn=nmaxi
Urne=[nn*3,0,0]
niveaup(nn,1,Urne,De,Vers)
print
print " # Fin du calcul #" |
Partager