stuck somewhere in recurionland

This commit is contained in:
Stephen McQuay 2012-10-30 23:23:24 -07:00
parent e7d56df745
commit 85e8be1bfa
3 changed files with 49 additions and 14 deletions

View File

@ -3,15 +3,45 @@ from random import randint
class CS235Maze(object):
def __init__(self, data, size=8):
self.data = data
self.size = size
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)):
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)
def _solve(self, x, y, z):
return False, set()
def solve(self):
return self._solve(0, 0, 0)
return self._solve(0, 0, 0, set())
def __str__(self):
r = derp_transpose(self.data)
@ -32,7 +62,7 @@ def make_maze(size=8):
for j in xrange(size):
r[i].append([randint(0, 1) for k in xrange(size)])
r[0][0][0] = 1
r[7][7][7] = 1
r[size - 1][size - 1][size - 1] = 1
return CS235Maze(r, size)
@ -40,7 +70,7 @@ def parse_maze(maze):
soilded = [[[int(i) for i in j.split()]
for j in k.split('\n')]
for k in maze.split('\n\n') if k]
cleaned = derp_transpose(soilded)
cleaned = derp_transpose(soilded, size=len(soilded))
return CS235Maze(cleaned)

View File

@ -83,9 +83,9 @@ class MazeTests(unittest.TestCase):
def test_at_indices(self):
assert self.m[7][0][0] == 0
assert self.m[7][3][0] == 1
assert self.m[7][0][1] == 0
assert self.m[3][3][3] == 1
assert self.m[5][2][6] == 1
assert self.m[7][5][4] == 0
assert self.m._data[7][0][0] == 0
assert self.m._data[7][3][0] == 1
assert self.m._data[7][0][1] == 0
assert self.m._data[3][3][3] == 1
assert self.m._data[5][2][6] == 1
assert self.m._data[7][5][4] == 0

5
samples/small.txt Normal file
View File

@ -0,0 +1,5 @@
1 0
0 0
1 0
1 1