Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中DFS图的生成_Python_Depth First Search_Maze - Fatal编程技术网

Python中DFS图的生成

Python中DFS图的生成,python,depth-first-search,maze,Python,Depth First Search,Maze,因此,我正试图更熟练地使用Python,并决定制作一个迷宫将是一件有趣的事情。我发现这涉及到如何做 create a CellStack (LIFO) to hold a list of cell locations set TotalCells = number of cells in grid choose a cell at random and call it CurrentCell set VisitedCells = 1 while

因此,我正试图更熟练地使用Python,并决定制作一个迷宫将是一件有趣的事情。我发现这涉及到如何做

   create a CellStack (LIFO) to hold a list of cell locations  
   set TotalCells = number of cells in grid  
   choose a cell at random and call it CurrentCell  
   set VisitedCells = 1  

    while VisitedCells < TotalCells 
        find all neighbors of CurrentCell with all walls intact   
        if one or more found 
            choose one at random  
            knock down the wall between it and CurrentCell  
            push CurrentCell location on the CellStack  
            make the new cell CurrentCell  
            add 1 to VisitedCells
        else 
            pop the most recent cell entry off the CellStack  
            make it CurrentCell
        endIf
    endWhile 
创建一个CellStack(后进先出)来保存单元格位置列表
set TotalCells=网格中的单元格数
随机选择一个单元格并将其命名为CurrentCell
设置VisitedCells=1
而VisitedCells
现在,我得到了下面的代码,尽管它并没有超越伪代码中显而易见的东西

class Cell:
    top_wall = 0
    bottom_wall = 0
    left_wall = 0
    right_wall = 0
    def knock_down(self,wall):
        if wall is 'top_wall' and self.top_wall is 0:
            self.top_wall = 1
        if wall is 'bottom_wall' and self.bottom_wall is 0:
            self.bottom_wall = 1
        if wall is 'left_wall' and self.left_wall is 0:
            self.left_wall = 1
        if wall is 'right_wall' and self.right_wall is 0:
            self.right_wall = 1
        else
            return 'Error: Wall Already Gone'

maze = [10][10]
CellStack = []          # LIFO stack to hold list of cell locations
TotalCells = 100        # Number of cells in grid
VisitedCells = 0        # Cells that have been visited
CurrentCell = 0         # The current cell

while VisitedCells < TotalCells:
类单元格:
顶墙=0
底墙=0
左墙=0
右墙=0
def拆卸(自身、墙壁):
如果墙为“顶墙”且self.top\u墙为0:
self.top_wall=1
如果墙为“底部墙”且self.bottom\u墙为0:
自底墙=1
如果墙为“左墙”且self.left\u墙为0:
self.left_wall=1
如果墙为“右墙”且self.right\u墙为0:
self.right_wall=1
其他的
返回“错误:墙已经消失”
迷宫=[10][10]
CellStack=[]#后进先出堆栈以保存单元格位置列表
TotalCells=100#网格中的单元数
VisitedCells=0#已访问的单元格
CurrentCell=0#当前单元格
当VisitedCells
我不确定这个类是否是做细胞的最好方法,但我还没有想到其他方法。然而,我在检查一个单元的邻居时遇到了一点问题。
查找CurrentCell的所有邻居,所有的墙都完好无损
让我有点紧张


如何检查单元格是否为相邻单元格?

您可以为每个单元格指定一个位置,存储为两个整数。如果两个整数是相邻的,那么两个单元格是相邻的

def isCellNeighbor(c1, c2):
   if abs(c1.x - c2.x) == 1: return True
   if abs(c1.y - c2.y) == 1: return True
   return False
如果两个单元的至少一个角与另一个单元接触,则上述两个单元视为相邻单元。你可以调整它以满足你的需要


PS:看一看

我在回答中说了这一点:)