# 模块化,重整程序
class Lstack():
def __init__(self,top=-1,full=20):
self._top = top
self._stack=[]
self.full = full
def is__full(self):
return self.full == self._top+1
def is_empty(self):
return self._top == -1
def push(self,x):
if self.full == self._top+1:
print("堆栈已满")
else:
self._stack.append(x)
self._top += 1
def pop(self):
if self._top == -1:
print("堆栈为空,不可弹出")
else:
top =self._top
self._top -= 1
return self._stack.pop(top)
def mark(maze,pos): # 主函数的标记模块
maze[pos[0],pose[1]]=2
# 主函数
def stack_find(maze,pos,end):
p =[(1,0), (0,1),(-1,0),(0,-1)] # 四个方向的增量
st = Lstack()
st.push(pos)
while not st.is_empty():
if pos == end:
print ('路径')
return True
else:
pos_ =pos
for i in range(4):
pos_new = pos+p[i]
if maze[pos_new[0],pos_new[1]] == 0: # 如果未访问
st.push(pos) # 前进
pos =pos_new
mark(maze,pos) # 标记
break
if pos == pos_:# 如果四个方向都没有
pos =st.pop() # 后退
print('NO found !')
#
import numpy
def test():
maze=
pos=
end =
stack_find(maze,pos,end)
非递归迷宫问题实现
最新推荐文章于 2021-07-11 13:00:19 发布