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 46 47 48 49 50 51 52 53 54 55 56
| # -*- coding: iso-8859-1 -*-
import copy
# nombre de lignes
n=5
#nombre de colonnes
m=5
#nombre de solutions
nbsol=0
#les voisins d'une case L=[x,y]
def voisins (L):
R=[]
x=L[0]
y=L[1]
x1=x-1
x2=x+1
y1=y-1
y2=y+1
if x1>=0:
R.append([x1,y])
if x2<n :
R.append([x2,y])
if y1>=0 :
R.append([x,y1])
if y2<m:
R.append([x,y2])
return R
#affiche les solutions commençant par le chemin L (liste de cases)
def printsolutions (L):
global nbsol
Dernier=L[-1]
VD=voisins(Dernier)
if len(L)==n*m-1:
Premier=L[0]
for v in VD:
if not(v in L):
R=copy.deepcopy(L)
R.append(v)
print R
nbsol=nbsol+1
else:
for v in VD:
if not (v in L):
Q=copy.deepcopy(L)
Q.append(v)
printsolutions(Q)
def main():
printsolutions([[n/2,m/2]])
print nbsol
if __name__ == '__main__':
main() |