Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Open Labyrinth by Bartlomiej_Szal
def getNeighbours(labirinth,actual):
neighbours = []
for i in [1,-1]:
if labirinth[actual[0] + i][actual[1]] == 0:
neighbours.append([actual[0] + i, actual[1]])
labirinth[actual[0] + i][actual[1]] = getDirectionFrom(actual,[actual[0] + i, actual[1]])
for i in [1,-1]:
if labirinth[actual[0]][actual[1] + i] == 0:
neighbours.append([actual[0], actual[1] + i])
labirinth[actual[0]][actual[1] + i] = getDirectionFrom(actual,[actual[0], actual[1] + i])
return neighbours
def getDirectionFrom(lastCell,newCell):
if lastCell[0] == newCell[0] - 1:
return 2
elif lastCell[0] == newCell[0] + 1:
return 3
elif lastCell[1] == newCell[1] - 1:
return 4
elif lastCell[1] == newCell[1] + 1:
return 5
# 2 - came from north
# 3 - came from south
# 4 - came from west
# 5 - came from east
def readPath(maze_map,end,start):
path = []
actual = end
while actual != start:
if maze_map[actual[0]][actual[1]] == 2:
actual = [actual[0]-1,actual[1]]
path.append('S')
elif maze_map[actual[0]][actual[1]] == 3:
actual = [actual[0]+1,actual[1]]
path.append('N')
elif maze_map[actual[0]][actual[1]] == 4:
actual = [actual[0],actual[1]-1]
path.append('E')
elif maze_map[actual[0]][actual[1]] == 5:
actual = [actual[0],actual[1]+1]
path.append('W')
path.reverse()
return path
def checkio(maze_map):
cells = []
start = [1,1]
end = [10,10]
maze_map[start[0]][start[1]] = 9
actual = start
nowhereToGo = 0
while actual != end and not nowhereToGo:
neighbours = getNeighbours(maze_map,actual)
if neighbours == [] and new == [start[0],start[1]]:
nowhereToGo = 1
else:
for i in neighbours:
cells.append(i)
new = cells[0]
cells.pop(0)
actual = new
path = readPath(maze_map,end,start)
return ''.join(path)
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, 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. 4, 2016