fix to keep track of path

This commit is contained in:
Stephen McQuay 2012-10-31 22:03:37 -07:00
parent c0be71036d
commit 091232a21c

View File

@ -12,7 +12,7 @@ class CS235Maze(object):
self._data = data self._data = data
self._size = len(data) self._size = len(data)
def _solve(self, x, y, z): def _solve(self, x, y, z, path=None):
if x < 0 or y < 0 or z < 0 or \ if x < 0 or y < 0 or z < 0 or \
x >= self._size or y >= self._size or z >= self._size: x >= self._size or y >= self._size or z >= self._size:
return False return False
@ -21,22 +21,27 @@ class CS235Maze(object):
elif x == self._size - 1 and y == self._size - 1 \ elif x == self._size - 1 and y == self._size - 1 \
and z == self._size - 1: and z == self._size - 1:
self._data[x][y][z] = PATH self._data[x][y][z] = PATH
path.append((x, y, z))
return True return True
else: else:
self._data[x][y][z] = PATH self._data[x][y][z] = PATH
if self._solve(x - 1, y, z) or \ if self._solve(x - 1, y, z, path) or \
self._solve(x + 1, y, z) or \ self._solve(x + 1, y, z, path) or \
self._solve(x, y - 1, z) or \ self._solve(x, y - 1, z, path) or \
self._solve(x, y + 1, z) or \ self._solve(x, y + 1, z, path) or \
self._solve(x, y, z - 1) or \ self._solve(x, y, z - 1, path) or \
self._solve(x, y, z + 1): self._solve(x, y, z + 1, path):
path.append((x, y, z))
return True return True
else: else:
self._data[x][y][z] = DEAD self._data[x][y][z] = DEAD
return False return False
def solve(self): def solve(self):
return self._solve(0, 0, 0) path = []
success = self._solve(0, 0, 0, path)
path.reverse()
return success, path
def __str__(self): def __str__(self):
r = derp_transpose(self._data) r = derp_transpose(self._data)