-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from adefemi171/cli-lazer-maze
used CLI to request matrix size
- Loading branch information
Showing
2 changed files
with
39 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,61 @@ | ||
from copy import deepcopy | ||
def get_coord(box_num, dim): | ||
row = box_num // dim | ||
col = box_num % dim | ||
return (row, col) | ||
|
||
grids = [] | ||
def populate_grid(grid, dim, pos): | ||
print(pos, len(grids), "HERE") | ||
if pos > dim*dim: return | ||
if pos == dim*dim: | ||
grids.append(list(grid)) | ||
for i in range(pos, dim*dim): | ||
row, col = get_coord(i, dim) | ||
for choice in ['L', 'R']: | ||
grid[row][col] = choice | ||
populate_grid(grid, dim, pos+1) | ||
grids.append(deepcopy(grid)) | ||
return | ||
|
||
row, col = get_coord(pos, dim) | ||
for choice in ['L', 'R']: | ||
grid[row][col] = choice | ||
populate_grid(grid, dim, pos+1) | ||
|
||
def createMaze(dim): | ||
grid = [[None] * dim for i in range(dim)] | ||
populate_grid(grid, dim, 0) | ||
|
||
def is_open_maze(grid, dim, col, row): | ||
if col >= 0 or col < dim and row >= dim: | ||
def is_open_maze(grid, dim, col, row, last_direction): | ||
if (col >= 0 or col < dim) and row >= dim: | ||
return True | ||
|
||
if row >= dim or row < 0 or col >= dim or col < 0: | ||
return False | ||
|
||
if grid[row][col] == 'L': | ||
return is_open_maze(grid, dim, row, col+1) | ||
if last_direction == 'R': return False | ||
if last_direction == 'L': | ||
return is_open_maze(grid, dim, row+1, col, 'L') | ||
return is_open_maze(grid, dim, row, col+1, 'L') | ||
else: | ||
return is_open_maze(grid, dim, row, col-1) | ||
if last_direction == 'L': | ||
return False | ||
if last_direction == 'R': | ||
return is_open_maze(grid, dim, row+1, col, 'R') | ||
return is_open_maze(grid, dim, row, col-1, 'R') | ||
|
||
return False | ||
|
||
def calculate_open_mazes(grid, dim): | ||
num_of_open_mazes = 0 | ||
for i in range(dim): | ||
if is_open_maze(grid, dim, i, 0): | ||
num_of_open_mazes += 1 | ||
|
||
return num_of_open_mazes | ||
if is_open_maze(grid, dim, i, 0, 'X'): | ||
return True | ||
return False | ||
# calculate_open_mazes([['R', 'R', 'R'], ['R', 'R', 'R'], ['R', 'R', 'R']], 3) | ||
|
||
if __name__ == '__main__': | ||
dimensions = int(input("please put the grid's dimensions: ")) | ||
createMaze(dimensions) | ||
result = 0 | ||
|
||
createMaze(3) | ||
createMaze(4) | ||
for grid in grids: | ||
if calculate_open_mazes(grid, dimensions) > 0: | ||
result += 1 | ||
|
||
for grid in grids: | ||
calculate_open_mazes(grid, 3) | ||
calculate_open_mazes(grid, 4) | ||
print("The number of open mazes is", result) |