import random
# 初始化迷宫地图
maze = [[0 for _ in range(3)] for _ in range(3)]
# 随机选择起点位置
start_row = random.randint(0, 2)
start_col = random.randint(0, 2)
maze[start_row][start_col] = 1
# 生成迷宫
for i in range(2):
for j in range(3):
if random.randint(0, 1) == 0:
next_row = start_row - 1 if j == 0 else start_row + 1 if j == 2 else start_row
next_col = start_col - 1 if i == 0 else start_col + 1 if i == 2 else start_col
if next_row >= 0 and next_row < 3 and next_col >= 0 and next_col < 3:
maze[next_row][next_col] = maze[start_row][start_col] + 1
start_row = next_row
start_col = next_col
else:
next_row = start_row - 1 if i == 0 else start_row + 1 if i == 2 else start_row
next_col = start_col - 1 if j == 0 else start_col + 1 if j == 2 else start_col
if next_row >= 0 and next_row < 3 and next_col >= 0 and next_col < 3:
maze[next_row][next_col] = maze[start_row][start_col] + 1
start_row = next_row
start_col = next_col
if i == 2 and j == 2:
break
if i == j:
break