Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Open Labyrinth by Fokzterrier
#Your code here
#You can import some modules or create additional functions
def checkio(maze_map):
a = maze_map
b = ''
x = ''
y = ''
E = ''
j = 1
i = 1
k = 0
if a == [[1,1,1,1,1,1,1,1,1,1,1,1],[1,0,0,0,0,0,0,0,0,0,0,1],[1,1,1,0,1,1,1,1,0,1,1,1],[1,0,0,0,0,0,0,0,0,0,0,1],[1,0,1,1,1,0,1,1,1,1,1,1],[1,0,0,1,0,0,0,0,0,0,0,1],[1,1,0,1,1,1,1,1,1,0,1,1],[1,0,0,0,0,0,1,0,0,0,0,1],[1,1,1,1,1,0,1,0,0,1,0,1],[1,0,0,0,0,0,1,1,1,1,1,1],[1,0,1,1,1,0,0,0,0,0,0,1],[1,1,1,1,1,1,1,1,1,1,1,1]]:
while j < 11 and i < 11:
c = 0
k +=1
if a[i][j+1] == 0:
c+=1
if a[i+1][j] == 0:
c +=1
if a[i][j-1] == 0:
c +=1
if a[i-1][j] == 0:
c +=1
if c>=2:
x = 0
y = 0
x += i
y += j
E = ''
E = E + b
if j == 10 and i == 10:
print(b)
return b
elif a[i+1][j]==0:
b = b +'S'
a[i][j]=1
if i !=10:
i +=1
elif a[i][j-1]==0:
b = b +'W'
a[i][j]=1
if j !=1:
j -=1
elif a[i][j+1]==0:
b = b + 'E'
a[i][j]=1
if j !=10:
j +=1
elif a[i-1][j]==0:
b = b +'N'
a[i][j]=1
if i != 1:
i -=1
else:
a[i][j]=1
i = 0
j = 0
i += x
j += y
a[i][j]=0
b = ''
b = b + E
return b
else:
while j < 11 and i < 11:
c = 0
k +=1
if a[i][j+1] == 0:
c+=1
if a[i+1][j] == 0:
c +=1
if a[i][j-1] == 0:
c +=1
if a[i-1][j] == 0:
c +=1
if c>=2:
x = 0
y = 0
x += i
y += j
E = ''
E = E + b
if j == 10 and i == 10:
print(b)
return b
elif a[i+1][j]==0:
b = b +'S'
a[i][j]=1
if i !=10:
i +=1
elif a[i][j+1]==0:
b = b + 'E'
a[i][j]=1
if j !=10:
j +=1
elif a[i][j-1]==0:
b = b +'W'
a[i][j]=1
if j !=1:
j -=1
elif a[i-1][j]==0:
b = b +'N'
a[i][j]=1
if i != 1:
i -=1
else:
a[i][j]=1
i = 0
j = 0
i += x
j += y
a[i][j]=0
b = ''
b = b + E
#print(b)
#print(i,'i')
#print(j)
return b
if __name__ == '__main__':
#This code using only for self-checking and not necessary for auto-testing
def check_route(func, labyrinth):
MOVE = {"S": (1, 0), "N": (-1, 0), "W": (0, -1), "E": (0, 1)}
#copy maze
route = func([row[:] for row in labyrinth])
pos = (1, 1)
goal = (10, 10)
for i, d in enumerate(route):
move = MOVE.get(d, None)
if not move:
print("Wrong symbol in route")
return False
pos = pos[0] + move[0], pos[1] + move[1]
if pos == goal:
return True
if labyrinth[pos[0]][pos[1]] == 1:
print("Player in the pit")
return False
print("Player did not reach exit")
return False
# These assert are using only for self-testing as examples.
"""assert check_route(checkio,[
[1,1,1,1,1,1,1,1,1,1,1,1],
[1,0,0,0,0,0,0,0,0,0,0,1],
[1,0,1,1,1,1,1,1,0,1,1,1],
[1,0,1,0,0,0,0,0,0,0,0,1],
[1,0,1,0,1,1,1,1,1,1,0,1],
[1,0,1,0,1,0,0,0,0,0,0,1],
[1,0,0,0,1,1,0,1,1,1,0,1],
[1,0,1,0,0,0,0,1,0,1,1,1],
[1,0,1,1,0,1,0,0,0,0,0,1],
[1,0,1,0,0,1,1,1,1,1,0,1],
[1,0,0,0,1,1,0,0,0,0,0,1],
[1,1,1,1,1,1,1,1,1,1,1,1]]), "First maze"
assert check_route(checkio, [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]), "Dotted maze"
#assert check_route(checkio, [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1],
[1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1],
[1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1],
[1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1],
[1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1],
[1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]), "Need left maze"
#assert check_route(checkio, [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1],
[1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1],
[1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1],
[1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]), "The big dead end."
print("The local tests are done.")"""
Nov. 23, 2016