Python-100-Days/公开课/文档/第06次公开课-算法入门系列2-在水一方/code/example03.py

62 lines
1.3 KiB
Python
Raw Normal View History

2019-07-03 21:57:12 +08:00
"""
迷宫寻路
"""
import random
import sys
WALL = -1
ROAD = 0
ROWS = 10
COLS = 10
def find_way(maze, i=0, j=0, step=1):
"""走迷宫"""
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()