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))

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.