if 0 <= i < ROWS and 0 <= j < COLS and maze[i][j] == 0:
maze[i][j] = step
if i == ROWS - 1 and j == COLS - 1:
print('=' * 20)
display(maze)
sys.exit(0)
find_way(maze, i + 1, j, step + 1)
find_way(maze, i, j + 1, step + 1)
find_way(maze, i - 1, j, step + 1)
find_way(maze, i, j - 1, step + 1)
maze[i][j] = ROAD
def reset(maze):
"""重置迷宫"""
for i in range(ROWS):
for j in range(COLS):
num = random.randint(1, 10)
maze[i][j] = WALL if num > 7 else ROAD
maze[0][0] = maze[ROWS - 1][COLS - 1] = ROAD
def display(maze):
"""显示迷宫"""
for row in maze:
for col in row:
if col == -1:
print('■', end=' ')
elif col == 0:
print('□', end=' ')
else:
print(f'{col}'.ljust(2), end='')
print()
def main():
"""主函数"""
maze = [[0] * COLS for _ in range(ROWS)]
reset(maze)
display(maze)
find_way(maze)
print('没有出路!!!')
if __name__ == '__main__':
main()
```
**说明:**上面的代码用随机放置围墙的方式来生成迷宫,更好的生成迷宫的方式请参考[《简单的使用回溯法生成 Tile Based 迷宫》](<https://indienova.com/indie-game-development/generate-tile-based-maze-with-backtracking/>)一文。