fixed solving of maze
This commit is contained in:
parent
85e8be1bfa
commit
52801d8ad7
@ -1,54 +1,50 @@
|
||||
import copy
|
||||
from random import randint
|
||||
|
||||
WALL = 0
|
||||
EMPTY = 1
|
||||
PATH = 2
|
||||
TEMP = 3
|
||||
|
||||
|
||||
class CS235Maze(object):
|
||||
def __init__(self, data):
|
||||
self._data = data
|
||||
self._size = len(data)
|
||||
|
||||
def _solve(self, x, y, z, visited):
|
||||
print x, y, z
|
||||
if all(i == self._size - 1 for i in (x, y, z)):
|
||||
def _solve(self, x, y, z):
|
||||
if x < 0 or y < 0 or z < 0 or \
|
||||
x >= self._size or y >= self._size or z >= self._size:
|
||||
return False
|
||||
elif self._data[x][y][z] != EMPTY:
|
||||
return False
|
||||
elif x == self._size - 1 and y == self._size - 1 \
|
||||
and z == self._size - 1:
|
||||
self._data[x][y][z] = PATH
|
||||
return True
|
||||
|
||||
if x - 1 >= 0 and (x - 1, y, z) not in visited:
|
||||
_v = copy.deepcopy(visited)
|
||||
_v.add((x - 1, y, z))
|
||||
return self._solve(x - 1, y, z, _v)
|
||||
if x + 1 < self._size and self._data[x + 1][y][z] and (x + 1, y, z) not in visited:
|
||||
_v = copy.deepcopy(visited)
|
||||
_v.add((x + 1, y, z))
|
||||
return self._solve(x + 1, y, z, _v)
|
||||
|
||||
if y - 1 >= 0 and (x, y - 1, z) not in visited:
|
||||
_v = copy.deepcopy(visited)
|
||||
_v.add((x, y - 1, z))
|
||||
self._solve(x, y - 1, z, _v)
|
||||
if y + 1 < self._size and self._data[x][y + 1][z] and (x, y + 1, z) not in visited:
|
||||
_v = copy.deepcopy(visited)
|
||||
_v.add((x, y + 1, z))
|
||||
return self._solve(x, y + 1, z, _v)
|
||||
|
||||
if z - 1 >= 0 and (x, y, z - 1) not in visited:
|
||||
_v = copy.deepcopy(visited)
|
||||
_v.add((x, y, z - 1))
|
||||
return self._solve(x, y, z - 1, _v)
|
||||
if z + 1 < self._size and self._data[x][y][z + 1] and (x, y, z + 1) not in visited:
|
||||
_v = copy.deepcopy(visited)
|
||||
_v.add((x, y, z + 1))
|
||||
return self._solve(x, y, z + 1, _v)
|
||||
else:
|
||||
self._data[x][y][z] = PATH
|
||||
if self._solve(x - 1, y, z) or \
|
||||
self._solve(x + 1, y, z) or \
|
||||
self._solve(x, y - 1, z) or \
|
||||
self._solve(x, y + 1, z) or \
|
||||
self._solve(x, y, z - 1) or \
|
||||
self._solve(x, y, z + 1):
|
||||
return True
|
||||
else:
|
||||
self._data[x][y][z] = TEMP
|
||||
return False
|
||||
|
||||
|
||||
def solve(self):
|
||||
return self._solve(0, 0, 0, set())
|
||||
return self._solve(0, 0, 0)
|
||||
|
||||
def __str__(self):
|
||||
r = derp_transpose(self.data)
|
||||
r = derp_transpose(self._data)
|
||||
rs = ''
|
||||
for i in xrange(self.size):
|
||||
for j in xrange(self.size):
|
||||
for k in xrange(self.size):
|
||||
for i in xrange(self._size):
|
||||
for j in xrange(self._size):
|
||||
for k in xrange(self._size):
|
||||
rs += '{} '.format(r[i][j][k])
|
||||
rs += '\n'
|
||||
rs += '\n'
|
||||
@ -63,7 +59,7 @@ def make_maze(size=8):
|
||||
r[i].append([randint(0, 1) for k in xrange(size)])
|
||||
r[0][0][0] = 1
|
||||
r[size - 1][size - 1][size - 1] = 1
|
||||
return CS235Maze(r, size)
|
||||
return CS235Maze(r)
|
||||
|
||||
|
||||
def parse_maze(maze):
|
||||
|
Loading…
Reference in New Issue
Block a user