简体   繁体   English

Python中的DFS图生成

[英]DFS Graph Generation in Python

So I'm trying to get a little more proficient with Python, and decided that making a maze would be a fun thing to know how to do. 因此,我试图更加精通Python,并决定做一个迷宫将是一件有趣的事情。 I found this page that goes over a bit of how to do it. 我发现此页面介绍了一些操作方法。

   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 

Now, I've got the following code, although it isn't much past the obvious stuff in the pseudocode. 现在,我有了以下代码,尽管它远没有超出伪代码中显而易见的内容。

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:

I'm not sure that the class is the best way to do the cells, but I haven't thought of another way to do it yet. 我不确定班级是做细胞的最好方法,但我还没有想到另一种做细胞的方法。 However, I've run into a bit of a problem for checking for the neighbors of a cell. 但是,在检查单元格的邻居时遇到了一个问题。 The find all neighbors of CurrentCell with all walls intact is throwing me for a bit of a loop. find all neighbors of CurrentCell with all walls intact的情况下find all neighbors of CurrentCell with all walls intact我陷入了一个循环。

How can you check whether cells are neighbors? 您如何检查小区是否是邻居?

You can give each cell a position, stored as two integers. 您可以为每个单元格指定一个位置,并存储为两个整数。 Then, two cells are neighbors if those integers are neighbors. 然后,如果两个整数是邻居,则两个单元为邻居。

def isCellNeighbor(c1, c2):
   if abs(c1.x - c2.x) == 1: return True
   if abs(c1.y - c2.y) == 1: return True
   return False

The above considers two cells as being neighbors if at least a corner of each one touches the other. 如果每个单元的至少一个角彼此接触,则以上将其视为相邻单元。 You can tweak it to suit your needs. 您可以根据需要进行调整。

PS: have a look at the amazing collection of maze algorithms PS:看看迷宫算法的惊人集合

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM