class Box:
def init(self,i1,j1):
self.i=i1
self.j=j1
self.pre=None
def mgpath(xi,yi,xe,ye):
global mg
dx=[-1,0,1,0]
dy=[0,1,0,-1]
qu=deque()
b=Box(xi,yi)
qu.appendleft(b)
mg[xi][yi]=-1
while len(qu)!=0:
b=qu.pop()
if b.i==xe and b.j==ye:
p=b
path=[]
while p!=None:
path.append("["+str(p.i)+","+str(p.j)+"]")
p=p.pre
for i in range(len(path)-1,-1,-1):
print(path[i],end='')
return True
for di in range(4):
i,j=b.i+dx[di],b.j+dy[di]
if mg[i][j]==0:
b1=Box(i,j)
b1.pre=b
qu.appendleft(b1)
mg[i][j]=-1
return False
#主程序设计
xi,yi=1,1
xe,ye=4,4
print("一条迷宫路径:",end='')
if not mgpath(xi,yi,xe,ye):
print("不存在迷宫路径")
为啥显示mgpath没有定义
print()
格式乱了,用代码块贴一下