forked from wjw12/python
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaze.py
More file actions
Latest commit
56 lines (53 loc) · 1.48 KB
/
Copy pathmaze.py
File metadata and controls
56 lines (53 loc) · 1.48 KB
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
classStack():
""" This class purpose is to append data to self.elems """
def__init__(self):
self.elems= [ ]
defis_empty(self):
returnself.elems== [ ]
deftop(self):
ifself.elems== [ ]:
raiseStackUnderflow
returnself.elems[len(self.elems)-1]
defpush(self, elem):
self.elems.append(elem)
defpop(self):
ifself.elems== [ ]:
raiseStackUnderflow
returnself.elems.pop()
classelement():
""" This class purpose is to initialize data and assigning them """
def__init__(self,x,y,status):
self.x=x
self.y=y
self.s=status
directions= ((0,1),(1,0),(0,-1),(-1,0))
maze= [[1,1,1,1,1,1],[1,0,1,0,1,1],[1,0,0,0,0,1],[1,0,1,0,1,1],[1,1,1,1,1,1]]
defmazePath (maze,x,y,end_x,end_y):
""" This function runs the algorithm of the maze.py """
st=Stack()
maze[x][y] =2
ele=element(x,y,-1)
st.push(ele)
whilenotst.is_empty():
top=st.top()
#print ("(%d,%d)"%(top.x,top.y))
whiletop.s<3:
top=st.top()
top.s+=1
new_x, new_y=top.x+directions[top.s][0], top.y+directions[top.s][1]
if (new_x, new_y) == (end_x, end_y):
print (new_x," ",new_y)
whilenotst.is_empty() :
ele=st.pop()
print (ele.x," ",ele.y)
returnTrue
ifmaze[new_x][new_y] ==0 :
newele=element(new_x,new_y,-1)
maze[new_x][new_y] =2
st.push(newele)
continue
st.pop()
print ("No way")
returnFalse
# This will run the whole script
mazePath(maze,1,1,3,3)