Python迷宫解决-使用什么体系结构?
我对python相当陌生,正在尝试构建一个迷宫解算器。我已经构建了一个算法,可以导航一条路线并打印它的路线(N/E/s/W),但我想将其移动到多条路线上。我应该使用什么样的体系结构来处理多个路由 我曾考虑使用类和子类来对不同的路由进行建模,以便子类继承它们的“存根”,但现在我想知道这是否有点过于复杂了。我看过关于递归的帖子,但不知道从哪里开始 有人有什么建议吗?如果有人感兴趣,我的代码如下:Python迷宫解决-使用什么体系结构?,python,recursion,maze,Python,Recursion,Maze,我对python相当陌生,正在尝试构建一个迷宫解算器。我已经构建了一个算法,可以导航一条路线并打印它的路线(N/E/s/W),但我想将其移动到多条路线上。我应该使用什么样的体系结构来处理多个路由 我曾考虑使用类和子类来对不同的路由进行建模,以便子类继承它们的“存根”,但现在我想知道这是否有点过于复杂了。我看过关于递归的帖子,但不知道从哪里开始 有人有什么建议吗?如果有人感兴趣,我的代码如下: # ==================================================
# =============================================================================
# MAZE
# =============================================================================
# Edit this grid to change the maze. Codes are:
# 0 = Walkable
# 1 = Wall
# 2 = Start
# 3 = Finish
maze = [[1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1],
[1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1],
[1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
# =============================================================================
# CLASSES AND METHODS
# =============================================================================
# Class which will hold all the routes
class Route():
used = []
def __init__(self):
self.directions = []
self.tracks = []
self.status = 'Pending'
# Class which stores position of 'marker' and 'start' in maze
class Pos():
def __init__(self, x, y):
self.x = x
self.y = y
self.coords = [] # Coordinates updated UPDATE_POS method
self.N = []
self.E = []
self.S = []
self.W = []
self.update_pos(x, y)
# When called, it updates values manually
def update_pos(self, x, y):
self.coords = [x, y] # Coordinates updated from x and y
self.N = [x, y-1, 'N'] # Directions define adjacents
self.E = [x+1, y, 'E']
self.S = [x, y+1, 'S']
self.W = [x-1, y, 'W']
# Find start of maze and set position
def find_start(self, maze):
for row in maze:
if 2 not in row:
continue
if 2 in row:
self.y = maze.index(row)
self.x = maze[maze.index(row)].index(2)
self.update_pos(self.x, self.y)
# Check if the index is within the maze
def valid_space(self, maze, index): # index in the form [x,y]
size = len(maze)
if index[0] < size and index[1] < size:
return True
else:
return False
# Looks for walkable spaces and moves the position to that space
def look_around(self, maze):
directions = [self.N, self.E, self.S, self.W]
for direction in directions:
# Eliminate invalid moves, used or outside maze
if self.valid_space(maze, direction):
if direction[0:2] in Route.used:
continue
# Look for walkable space
if maze[direction[1]][direction[0]] == 0:
self.x = direction[0]
self.y = direction[1]
self.update_pos(self.x, self.y)
route1.directions.append(direction[2])
Route.used.append(self.coords)
break
# Look for finish
if maze[direction[1]][direction[0]] == 3:
self.x = direction[0]
self.y = direction[1]
self.update_pos(self.x, self.y)
route1.directions.append(direction[2])
Route.used.append(self.coords)
route1.status = 'Finished'
else:
continue
# =============================================================================
# MAIN
# =============================================================================
# Initialise position and route
position = Pos(0, 0)
start_pos = Pos(0, 0)
route1 = Route()
# Change position to start of maze and print
start_pos.find_start(maze)
position.find_start(maze) # Set position to start
print('Solving...')
while route1.status != 'Finished':
position.look_around(maze)
print('''The directions to the finish are: ''')
print(route1.directions)
#=============================================================================
#迷宫
# =============================================================================
#编辑此网格以更改迷宫。代码为:
#0=可步行
#1=墙
#2=开始
#3=完成
迷宫=[[1,1,1,3,1,1,1,1,1,1,1,1,1],
[1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1],
[1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1],
[1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
# =============================================================================
#类和方法
# =============================================================================
#类,该类将保存所有路由
类路由():
已用=[]
定义初始化(自):
self.directions=[]
self.tracks=[]
self.status='Pending'
#类,该类存储迷宫中“标记”和“开始”的位置
类Pos():
定义初始化(self,x,y):
self.x=x
self.y=y
self.coords=[]坐标更新更新位置方法
self.N=[]
self.E=[]
self.S=[]
self.W=[]
自我更新位置(x,y)
#调用时,它会手动更新值
def更新位置(自身、x、y):
self.coords=[x,y]#从x和y更新坐标
self.N=[x,y-1,'N']#方向定义邻接
self.E=[x+1,y,'E']
self.S=[x,y+1,'S']
self.W=[x-1,y,'W']
#找到迷宫的起点并设置位置
def查找_启动(自我、迷宫):
对于迷宫中的行:
如果2不在行:
持续
如果第2行:
self.y=迷宫索引(行)
self.x=迷宫[迷宫索引(行)]。索引(2)
自我更新位置(自我x、自我y)
#检查索引是否在迷宫中
定义有效_空间(self、maze、index):#索引形式为[x,y]
尺寸=透镜(迷宫)
如果索引[0]<大小,而索引[1]<大小:
返回真值
其他:
返回错误
#查找可行走空间并将位置移动到该空间
def环顾四周(自我、迷宫):
方向=[self.N,self.E,self.S,self.W]
对于方向中的方向:
#消除无效移动,使用或外部迷宫
如果自有效_空间(迷宫、方向):
如果路线中的方向[0:2]已使用:
持续
#寻找可行走的空间
如果迷宫[方向[1]][方向[0]]==0:
self.x=方向[0]
self.y=方向[1]
自我更新位置(自我x、自我y)
route1.directions.append(方向[2])
Route.used.append(self.coords)
打破
#寻找终点
如果迷宫[方向[1]][方向[0]]==3:
self.x=方向[0]
self.y=方向[1]
自我更新位置(自我x、自我y)
route1.directions.append(方向[2])
Route.used.append(self.coords)
route1.status='Finished'
其他:
持续
# =============================================================================
#主要
# =============================================================================
#初始化位置和路线
位置=位置(0,0)
开始位置=位置(0,0)
路由1=路由()
#将位置更改为迷宫开始并打印
开始位置查找开始(迷宫)
位置。找到开始(迷宫)#设置开始位置
打印('解决…')
而route1.status!='完成':
位置。环顾四周(迷宫)
打印(''完成的方向是:'')
打印(路线1.方向)
您真的不需要比迷宫中跟踪路径的列表更复杂的数据结构。您可能希望使用列表列表来跟踪找到的路径
当您开发解决方案时,考虑如何跟踪已经存在的位置。
欢迎使用StAdvExcel。请阅读并遵循帮助文档中的发布指南。在这里申请。StackOverflow不是设计、编码、研究或教程服务。。这通常意味着你需要的是半个小时的时间和当地的导师在一起,或是走一遍教程,而不是堆积如山。@Carcigenicate:谢谢。令人惊讶的是,以前没有人(包括我)注意到这一点。不,这是我到目前为止的代码,我只是需要一个关于如何将多个路由包含到其中的建议?(如果你想知道的话,我是这样格式化的)你有没有一个算法,只需要随机路由,然后标记它们完成?它能识别已经访问过的数组索引吗?不,你只需要在跟踪posit时小心