import random from collections import deque def generate_maze(w, h, seed=0): """ Generate a randomized perfect maze using depth-first search. Args: w (int): width of the maze in cells (number of columns). h (int): height of the maze in cells (number of rows). Returns: list[list[int]]: 2D grid representing the maze where 1 indicates a wall and 0 indicates an open cell. The start is at (0,0) and the goal is at (w-1,h-1). """ maze = [[1]*w for _ in range(h)] rng = random.Random(seed) if seed else None def dfs(x, y): maze[y][x] = 0 dirs = [(0,2),(2,0),(0,-2),(-2,0)] (rng.shuffle(dirs) if rng is not None else random.shuffle(dirs)) for dx,dy in dirs: nx,ny = x+dx,y+dy if 0<=nx