Import random#warning:x and y confusingsx = 10sy = 10dfs = [[0 for Col. Range (SX)] for row in range (sy)]maze = [['] fo R col in range (2*sx+1)] ' for row ' in range (2*sy+1)] #1: Up 2:down 3:left 4:rightoperation = {1: (0,-1), 2: (0,1), 3: ( -1,0), 4: (1,0) }direction = [1, 2, 3, 4]stack = []for i in Range (2*sx+1): if i%2 = = 0:for J in range (2*sx+1): maze[ I][J] = ' # ' for I in Range (2*sy+1): if i%2 = = 0:for J in range (2*sy+1): maze[j][i] = ' # ' Def show (Grap h): For I in Graph:for J in I:print J, Printdef Showrouter (stack): Rgragh = [[0] for Col i N Range (SX)] for row in range (SY) "for" (x, Y) in stack:rgragh[y][x] = 1 show (Rgragh) Printdef Generatemaz E (start): x, y = start dfs[y][x] = 1 random.shuffle (direction) for D-direction:px, py = (x + y for x, y in Zip (start, operation[d])) if PX < 0 or px >= sx or py < 0 or py >= sy:pass E Lse:if dfs[pY][PX] is not 1:mx = 2*x + 1 my = 2*y + 1 if d = = 1:maze [My-1] [MX] = ' elif d = = 2:maze[my+1][mx] = ' elif D = = 3: Maze[my][mx-1] = ' elif d = = 4:maze[my][mx+1] = ' Generatemaze (( px,py)) Generatemaze ((0,0)) show (Dfs) show (maze)
Realization of Maze--python with depth-first algorithm