stuck somewhere in recurionland
This commit is contained in:
parent
e7d56df745
commit
85e8be1bfa
@ -3,15 +3,45 @@ from random import randint
|
|||||||
|
|
||||||
|
|
||||||
class CS235Maze(object):
|
class CS235Maze(object):
|
||||||
def __init__(self, data, size=8):
|
def __init__(self, data):
|
||||||
self.data = data
|
self._data = data
|
||||||
self.size = size
|
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):
|
def solve(self):
|
||||||
return self._solve(0, 0, 0)
|
return self._solve(0, 0, 0, set())
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
r = derp_transpose(self.data)
|
r = derp_transpose(self.data)
|
||||||
@ -32,7 +62,7 @@ def make_maze(size=8):
|
|||||||
for j in xrange(size):
|
for j in xrange(size):
|
||||||
r[i].append([randint(0, 1) for k in xrange(size)])
|
r[i].append([randint(0, 1) for k in xrange(size)])
|
||||||
r[0][0][0] = 1
|
r[0][0][0] = 1
|
||||||
r[7][7][7] = 1
|
r[size - 1][size - 1][size - 1] = 1
|
||||||
return CS235Maze(r, size)
|
return CS235Maze(r, size)
|
||||||
|
|
||||||
|
|
||||||
@ -40,7 +70,7 @@ def parse_maze(maze):
|
|||||||
soilded = [[[int(i) for i in j.split()]
|
soilded = [[[int(i) for i in j.split()]
|
||||||
for j in k.split('\n')]
|
for j in k.split('\n')]
|
||||||
for k in maze.split('\n\n') if k]
|
for k in maze.split('\n\n') if k]
|
||||||
cleaned = derp_transpose(soilded)
|
cleaned = derp_transpose(soilded, size=len(soilded))
|
||||||
return CS235Maze(cleaned)
|
return CS235Maze(cleaned)
|
||||||
|
|
||||||
|
|
||||||
|
@ -83,9 +83,9 @@ class MazeTests(unittest.TestCase):
|
|||||||
|
|
||||||
|
|
||||||
def test_at_indices(self):
|
def test_at_indices(self):
|
||||||
assert self.m[7][0][0] == 0
|
assert self.m._data[7][0][0] == 0
|
||||||
assert self.m[7][3][0] == 1
|
assert self.m._data[7][3][0] == 1
|
||||||
assert self.m[7][0][1] == 0
|
assert self.m._data[7][0][1] == 0
|
||||||
assert self.m[3][3][3] == 1
|
assert self.m._data[3][3][3] == 1
|
||||||
assert self.m[5][2][6] == 1
|
assert self.m._data[5][2][6] == 1
|
||||||
assert self.m[7][5][4] == 0
|
assert self.m._data[7][5][4] == 0
|
||||||
|
5
samples/small.txt
Normal file
5
samples/small.txt
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
1 0
|
||||||
|
0 0
|
||||||
|
|
||||||
|
1 0
|
||||||
|
1 1
|
Loading…
Reference in New Issue
Block a user