通过消除路口来解决迷宫



我们正在尝试制作一个程序,通过识别所有路口并消除不通向入口的路口来解决任何迷宫。我们设法创建了这样一个程序,但我们正在努力让点连接起来以创建正确的路径。有没有人知道如何做到这一点,因为我们没有线索......结果的图片,但点不是用一条线连接起来的

迷宫基本上是一个 numpy 数组中的 (n(x(n( 网格,带有墙壁(真(和路径(假( 参见:从变量资源管理器中看到的迷宫图片

import numpy as np
import maze_utils as mu
import matplotlib.pyplot as plt
size = 101
maze, start = mu.make_maze(size)
start = [start[1],start[0]]
#------------------------------------------------------------------------------
def junctions_finder(maze, size, start):
junctions = [start]
end = []
for y, row in enumerate(maze):
for x, column in enumerate(row):
if maze[x,y] == False:
if x == 0 or x == (size-1) or y == 0 or y == (size-1):
junctions.append([y,x])
end.append([y,x])

while True:
if x+1 < size and y+1 < size and
maze[x+1,y] == False and maze[x,y+1] == False
or x+1 < size and y-1 > 0 and
maze[x+1,y] == False and maze[x,y-1] == False
or x-1 > 0 and y-1 > 0 and
maze[(x-1),y] == False and maze[x,(y-1)] == False
or x-1 > 0 and y+1 < size and
maze[(x-1),y] == False and maze[x,(y+1)] == False:
junctions.append([y,x])
break
else:
break
return junctions, end
#------------------------------------------------------------------------------
def eliminate_coor(junctions, end, start):
eliminated = []
for row in junctions:
a = row[1]
b = row[0]
connections = 0
U = False
D = False
L = False
R = False
UW = False
DW = False
LW = False
RW = False
SE = False
if row == start or row == end[0]:
connections = 2
SE = True
for i in range(1,size):
if SE == False:
if a+i <= size-1 and DW == False and D == False:
if maze[a+i, b] == True:
DW = True
else:
for coor in junctions:
if [coor[1],coor[0]] == [a+i,b]:
connections = connections + 1
D = True

if a-i >= 0 and UW == False and U == False:
if maze[a-i, b] == True:
UW = True
else:
for coor in junctions:
if [coor[1],coor[0]] == [a-i,b]:
connections = connections + 1
U = True

if b+i <= size-1 and RW == False and R == False:
if maze[a, b+i] == True:
RW = True
else:
for coor in junctions:
if [coor[1],coor[0]] == [a,b+i]:
connections = connections + 1
R = True

if b-i >= 0 and LW == False and L == False:
if maze[a, b-i] == True:
LW = True
else:
for coor in junctions:
if [coor[1],coor[0]] == [a,b-i]:
connections = connections + 1
L = True
if connections < 2:
eliminated.append([b,a])
return eliminated
#------------------------------------------------------------------------------
def junction_remover(junctions, eliminated):
counter = 0
for index, row in enumerate(junctions):
for erow in (eliminated):
if erow == row:
junctions[index] = -1
counter = counter + 1
for times in range(counter):
junctions.remove(-1)
return junctions, counter
#------------------------------------------------------------------------------
junctions, end = junctions_finder(maze, size, start)
counter = 1
while counter > 0:
eliminated = eliminate_coor(junctions, end, start)
junctions, counter = junction_remover(junctions, eliminated)
start = [start[1],start[0]]     
junctions.pop(0)
pyjunc = np.array(junctions)
mu.plot_maze(maze, start=start)
plt.plot(pyjunc[:,0], pyjunc[:,1], 'o')

plt.plot(pyjunc[:,0], pyjunc[:,1]( 将连接这些点...或者你的意思是你有一个无法追踪的错误?在你的照片中,似乎有一个开始,但没有结束,所以它可以追溯到开始?

plt.plot(pyjunc[:,0], pyjunc[:,1], 'o')

使用圆圈标记'o'绘制列表中的数据点。但您尚未定义线型。

最快的方法是将其添加为格式简写:

plt.plot(pyjunc[:,0], pyjunc[:,1], 'o-')

它说使用圆圈标记和实线'-'.

将其扩展到matplotlib如何解释它,您可以编写:

plt.plot(pyjunc[:,0], pyjunc[:,1], marker='o', linestyle='-')

您可以查看完整的文档,了解自定义绘图plt.plot更多方法

最新更新