-
Notifications
You must be signed in to change notification settings - Fork 0
/
maze3.py
61 lines (53 loc) · 1.76 KB
/
maze3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import random
import numpy as np
from matplotlib import pyplot as plt
import matplotlib.cm as cm
num_rows = int(input("Rows: ")) # number of rows
num_cols = int(input("Columns: ")) # number of columns
# The array M is going to hold the array information for each cell.
# The first four coordinates tell if walls exist on those sides
# and the fifth indicates if the cell has been visited in the search.
# M(LEFT, UP, RIGHT, DOWN, CHECK_IF_VISITED)
M = np.zeros((num_rows,num_cols,5), dtype=np.uint8)
# The array image is going to be the output image to display
image = np.zeros((num_rows*10,num_cols*10), dtype=np.uint8)
# Set starting row and column
r = 0
c = 0
history = [(r,c)] # The history is the stack of visited locations
while history:
M[r,c,4]=1
check=[]
if c > 0 and M[r,c-1,4] == 0:
check.append('L')
if r > 0 and M[r-1,c,4] == 0:
check.append('U')
if c < num_cols-1 and M[r,c+1,4] == 0:
check.append('R')
if r < num_rows-1 and M[r+1,c,4] == 0:
check.append('D')
if len(check):
history.append([r,c])
move_direction=random.choice(check)
if move_direction == 'L':
M[r,c,0] = 1
c = c-1
M[r,c,2] = 1
if move_direction == 'U':
M[r,c,1] = 1
r = r-1
M[r,c,3] = 1
if move_direction == 'R':
M[r,c,2] = 1
c = c+1
M[r,c,0] = 1
if move_direction == 'D':
M[r,c,3] = 1
r = r+1
M[r,c,1] = 1
else: # If there are no valid cells to move to.
# retrace one step back in history if no move is possible
r,c = history.pop()
# Open the walls at the start and finish
M[0,0,0] = 1
M[num_rows-1,num_cols-1,2] = 1