labyrinthe.py

Created by nicolas-patrois

Created on February 09, 2019

2.33 KB

Chemin dans un labyrinthe.


from kandinsky import set_pixel,color

laby=(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
      1,0,1,0,0,0,1,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
      1,0,1,1,1,0,1,0,1,0,1,0,1,1,0,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,
      1,0,1,0,0,0,0,0,1,0,1,1,1,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,1,
      1,0,1,0,1,1,1,0,1,0,0,0,0,0,1,1,0,0,0,1,1,1,1,1,1,1,1,1,0,1,0,1,
      1,0,1,0,0,0,1,0,1,0,1,1,1,0,0,1,0,1,0,1,0,0,0,0,0,0,0,1,0,1,0,1,
      1,0,1,1,1,0,1,0,1,0,0,0,1,1,0,1,0,1,0,0,0,1,1,1,1,1,0,1,0,1,0,1,
      1,0,0,0,1,0,1,0,1,0,1,1,1,0,0,1,0,1,0,1,0,1,0,0,0,1,0,1,0,1,0,1,
      1,1,1,0,1,0,1,1,1,0,1,0,1,0,1,1,0,1,0,1,0,0,0,1,0,1,0,1,0,1,0,1,
      1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,1,0,1,0,1,1,1,0,1,0,1,0,1,0,1,
      1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,0,1,0,1,0,1,0,0,0,1,0,1,0,1,0,1,
      1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,1,1,1,1,1,0,1,0,1,0,1,
      1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,0,0,0,0,0,0,0,1,0,1,0,1,
      1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,1,0,1,
      1,0,1,0,1,1,1,1,1,1,1,1,1,1,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,1,
      1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,1,1,1,1,1,1,1,1,1,1,1,0,1,
      1,0,1,0,1,0,1,1,1,1,1,1,1,1,0,1,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,1,
      1,0,1,0,1,0,1,0,0,0,0,0,0,0,0,1,0,1,0,1,0,1,1,1,1,1,1,1,1,1,0,1,
      1,0,1,0,1,0,1,0,1,1,1,1,1,1,1,1,0,1,0,1,0,0,0,0,0,0,0,0,0,1,0,1,
      1,0,1,0,1,0,1,0,0,0,1,0,0,0,0,1,0,1,0,1,1,1,1,1,1,1,1,1,0,0,0,1,
      1,0,0,0,1,0,0,0,1,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,
      1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)

t=int(((320*220)/len(laby))**.5)
h,v=320//t,220//t
precedent=[None for _ in laby]

def carre(n,couleur=0):
  y,x=divmod(n,h)
  for i in range(t):
    for j in range(t):
      set_pixel(x*t+i,y*t+j,couleur)

def dessine():
  for i,e in enumerate(laby):
    if e:
      carre(i,color(100,100,100))

dessine()
debut=h+1
fin=h*v-h-2
carre(debut,color(200,0,0))
carre(fin,color(0,200,0))
afaire={debut}

while afaire:
  temp=set()
  for n in afaire:
    for m in (n-1,n+1,n-h,n+h):
      if laby[m]==0:
        if m not in afaire:
          if precedent[m]==None:
            temp.add(m)
            precedent[m]=n
  afaire=set(temp)

cour=fin
while cour!=None:
  carre(cour,color(0,0,200))
  cour=precedent[cour]
carre(debut,color(200,0,0))
carre(fin,color(0,200,0))