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