Sei sulla pagina 1di 5

# -*- coding: utf-8 -*-

"""
Created on Sat Sep 9 08:49:29 2017

@author: Pinkman
"""
import numpy as np
import matplotlib.pyplot as mat

permutation = [(1, 2, 3, 4), (1, 2, 4, 3), (1, 3, 2, 4), (1, 3, 4, 2),


(1, 4, 2, 3), (1, 4, 3, 2), (2, 1, 3, 4), (2, 1, 4, 3), (2, 3, 1, 4),
(2, 3, 4, 1), (2, 4, 1, 3), (2, 4, 3, 1), (3, 1, 2, 4), (3, 1, 4, 2),
(3, 2, 1, 4), (3, 2, 4, 1), (3, 4, 1, 2), (3, 4, 2, 1), (4, 1, 2, 3),
(4, 1, 3, 2), (4, 2, 1, 3), (4, 2, 3, 1), (4, 3, 1, 2), (4, 3, 2, 1),
(1, 2, 3, 4), (1, 2, 4, 3), (1, 3, 2, 4), (1, 3, 4, 2),
(1, 4, 2, 3), (1, 4, 3, 2), (2, 1, 3, 4), (2, 1, 4, 3), (2, 3, 1, 4),
(2, 3, 4, 1), (2, 4, 1, 3), (2, 4, 3, 1), (3, 1, 2, 4), (3, 1, 4, 2),
(3, 2, 1, 4), (3, 2, 4, 1), (3, 4, 1, 2), (3, 4, 2, 1), (4, 1, 2, 3),
(4, 1, 3, 2), (4, 2, 1, 3), (4, 2, 3, 1), (4, 3, 1, 2), (4, 3, 2, 1)]

maze = mat.imread('challenge.png')
size=np.shape(maze)

def deadend():
x,y,c,d=1,1,0,1
while d!=0:
d=0
x=1
while x<=size[0]-2:
y=1
while y<=size[1]-2:
if maze[x,y]==0:
y+=1
continue
if maze[x+1,y]==0:
c+=1
if maze[x+1,y-1]==0:
c+=1
if maze[x,y-1]==0:
c+=1
if maze[x-1,y-1]==0:
c+=1
if maze[x-1,y]==0:
c+=1
if maze[x-1,y+1]==0:
c+=1
if maze[x,y+1]==0:
c+=1
if maze[x+1,y+1]==0:
c+=1
if c==7:
maze[x,y]=0
d+=1
if maze[x+1,y]==1:
maze[x+1,y]=0
elif maze[x,y+1]==1:
maze[x,y+1]=0
elif maze[x-1,y]==1:
maze[x-1,y]=0
elif maze[x,y-1]==1:
maze[x,y-1]=0
y+=1
c=0
x+=1

def nadaananNa():
c=0
if maze[x+1,y]==3:
c+=1
if maze[x,y-1]==3:
c+=1
if maze[x-1,y]==3:
c+=1
if maze[x,y+1]==3:
c+=1
return c

def intersection():
c=0
if maze[x+1,y]==1:
c+=1
if maze[x,y-1]==1:
c+=1
if maze[x-1,y]==1:
c+=1
if maze[x,y+1]==1:
c+=1
return c

def choice(x,y,d,inte):
if maze[x+1,y] == 1:
maze[x+1,y]=3
x,y=x+1,y
d+=1
elif maze[x,y+1]==1:
maze[x,y+1]=3
x,y=x,y+1
d+=1
elif maze[x-1,y] == 1:
maze[x-1,y]=3
x,y=x-1,y
d+=1
elif maze[x,y-1]==1:
maze[x,y-1]=3
x,y=x,y-1
d+=1
return x,y,d,inte

def left(x,y,d,inte):
if maze[x,y-1] == 1:
maze[x,y-1]=3
x,y=x,y-1
d+=1
return x,y,d,inte
def right(x,y,d,inte):
if maze[x,y+1]==1:
maze[x,y+1]=3
x,y=x,y+1
d+=1
return x,y,d,inte
def down(x,y,d,inte):
if maze[x+1,y] == 1:
maze[x+1,y]=3
x,y=x+1,y
d+=1
return x,y,d,inte
def up(x,y,d,inte):
if maze[x-1,y]==1:
maze[x-1,y]=3
x,y=x-1,y
d+=1
return x,y,d,inte

bigperm=0
lengths, coordinates=[],[]
print ("Mej matagal lang but it works!")
while bigperm<24:
maze = mat.imread('challenge.png')
size=np.shape(maze)
inte=[]
gr=0
p,save, Iperm=[],[],[]
perm=0
bgn=1
x,d,z,v,ct, n=0,1,0,0,0, 0
fin=1
deadend()

while x<=size[0]-2:
y=0
while y<=size[1]-2:
if maze[x,y]==1:
z=1
break
y+=1
if z==1:
break
x+=1

while (x!=size[0]-2) and y!=(size[1]-2):


while (d!=0) and (x<=size[0]-2) and y<=(size[1]-2):
d=0
maze[x,y]=3
p.append((x,y))
n=intersection()
pas=nadaananNa()
if pas>1:
break
if n>1:
if gr!=0:
if inte[len(inte)-1]==save:
bgn=0
if bgn==1:
inte.append((x,y))
Iperm.append(0)
gr+=1
bgn=1
Ichoose = permutation[perm+bigperm]
ct=0
while ct<=3:
if Ichoose[ct]==1:
x,y,d,inte=down(x,y,d,inte)
elif Ichoose[ct]==2:
x,y,d,inte=right(x,y,d,inte)
elif Ichoose[ct]==3:
x,y,d,inte=left(x,y,d,inte)
elif Ichoose[ct]==4:
x,y,d,inte=up(x,y,d,inte)
if x>(size[0]-2) or y>(size[0]-2):
ct=3
ct+=1
elif n==1:
x,y,d,inte=choice(x,y,d,inte)
# print (inte)
z=0
v=0
ierase =0

if d==0 and (x!=size[0] or x!=0 or y!=0 or y!=(size[1])):


while v< len(p):
save=inte[len(inte)-1]
if inte[len(inte)-1]==p[v]:
while len(p)!=v:
maze[p[v]]=1
del p[v]
ierase = 1
break
v+=1
# x,y=inte[len(inte)-1]
d=1
if ierase==1:
perm+=1
Iperm[len(inte)-1]=perm
if Iperm[len(inte)-1] ==24:
perm=0
x,y=save
j=0
while j<len(inte):
if save==inte[j]:
del inte[j]
del Iperm[j]
j+=1
v=0
dont=0
if len(inte)==0:
dont=1
break
x,y=inte[len(inte)-1]

if dont==1:break
lengths.append(len(p))
coordinates.append(p)
bigperm+=1
lengths=np.unique(lengths).tolist()
coordinates=np.unique(coordinates).tolist()
minIndex = [i for i, q in enumerate(lengths) if q == min(lengths)]
counter=0
while counter<3:
count=0
data=coordinates[counter]
maze = mat.imread('challenge.png')
while count<len(data):
maze[data[count]]=3
count+=1
if counter==0:
print ("Shortest: " +str(coordinates[minIndex[0]])+ "\nLength: "
+str(lengths[minIndex[0]]))
mat.figure()
mat.title("Shortest")
mat.imshow(maze, cmap='binary_r')
if counter==1:
print ("1: " +str(coordinates[counter])+ "\nLength: "
+str(lengths[counter]))
mat.figure()
mat.title("1")
mat.imshow(maze, cmap='binary_r')
if counter==2:
print ("2: " +str(coordinates[counter])+ "\nLength: "
+str(lengths[counter]))
mat.figure()
mat.title("2")
mat.imshow(maze, cmap='binary_r')
counter+=1

Potrebbero piacerti anche