コード例 #1
0
ファイル: cylinder_demo.py プロジェクト: econrad003/yaMazeImp
def tests(grid):
    print(grid.unicode())

    stats = Maze_Statistics(grid)
    v = stats.size()
    p, w, e = stats.Euler_edge_counts()

    k, _ = Helper.find_components(grid)
    print("%s: %d cells" % (stats.name, v) + \
        ", %d passages, %d walls, %d edges" % (p//2, w//2, e//2) + \
        ", %d maze components" % k)
    degseq = stats.degree_counts()
    print("  Degree sequence: " + sorted_degrees(degseq))

    # The maze is a tree if and only if both of the following
    # are true:
    #   (a) the maze is passage-connected, and
    #   (b) the number of passages is one less than the number
    #       of cells.

    assert v is 35, "Vertex count error (got %s, expected v=5x7=35)" % v
    assert p is 68, "Passage count error (got %s, expected p=v-1=34)" % (p //
                                                                         2)
    assert e is 126, "Edge count error (got %s, expected e=63)" % (e // 2)
    assert w is 58, "Wall count error (got %s, expected w=e-p=29)" % (w // 2)
    assert k is 1, "The maze is disconnected (got k=%d, expected k=1)" % k
コード例 #2
0
 def components(self, maze, v, e):
     """component count and characteristic"""
     k, _ = Helper.find_components(maze)
     key = 'k'
     if key not in self.legend:
         self.make_key(key, "Number of components (k)")
     self.make_moments(key, k)
     chi = v - e - k
     key = 'chi'
     if key not in self.legend:
         self.make_key(key, "Maze characteristic (v-e-k)")
     self.make_moments(key, chi)
     return k
コード例 #3
0
def check_maze(grid):
    """check that the maze is perfect

    We want the maze to be a spanning tree of the grid.  If v is the
    number of vertices, e the number of edges, and k the number of
    components, we want to satisfy both the following conditions:
        (1) k = 1 (the maze is connected)
        (2) v = e + 1 (every edge is a bridge, given k=1)
    Since edges are counted in the manner of Euler, we count each edge
    twice.

    Note: Loops are not counted here with multiplicity, but the
    assertion will fail (as it should) if any loops occur.
    """
    m, n = grid.rows, grid.cols
    v = m * n                             # number of vertices
    e = 0                                 # number of arcs (2e)
    for cell in grid.each():
        e += len(cell.arcs)                   # Euler counting
    k, _ = Helper.find_components(grid)
        # note: e is twice the number of edges
    assert e + 2 == 2 * v and k == 1, \
        "v=%d, 2*e=%d, k=%d - not a tree" % (v, e, k)