【Python】迷路の最短経路を出力する

スタートからゴールまでの距離を2次元配列で表現して、
最短経路の通路を「 」(半角スペース)から「*」に置き換えて出力する。

import sys
import random

class Maze():
  PATH = 0
  WALL = 1

  def __init__(self, width, height, seed=0):
    self.width = width
    self.height = height
    if self.width < 5 or self.height < 5:
      sys.exit()
    if self.width % 2 == 0:
      self.width += 1
    if self.height % 2 == 0:
      self.height += 1
    self.maze = [[self.PATH for x in range(width)] for y in range(height)]
    self.dist = [[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
                 [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
                 [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
                 [-1, 10,  9,  8,  7,  6,  5,  4, -1, -1, -1, -1, -1, -1, -1],
                 [-1, 11, -1, -1, -1, -1, -1,  3, -1, -1, -1, -1, -1, -1, -1],
                 [-1, 12, -1, -1, -1,  0,  1,  2, -1, -1, -1, -1, -1, -1, -1],
                 [-1, 13, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
                 [-1, 14, 15, 16, 17, 18, -1, -1, -1, -1, -1, -1, -1, -1, -1],
                 [-1, -1, -1, -1, -1, 19, -1, -1, -1, -1, -1, -1, -1, -1, -1],
                 [-1, -1, -1, -1, -1, 20, 21, 22, 23, 24, -1, -1, -1, -1, -1],
                 [-1, -1, -1, -1, -1, -1, -1, -1, -1, 25, -1, -1, -1, -1, -1],
                 [-1, -1, -1, -1, -1, -1, -1, -1, -1, 26, -1, -1, -1, -1, -1],
                 [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
                 [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
                 [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1]]
    self.start = [1, 1]
    self.goal = [self.width-2, self.height-2]
    random.seed(seed)

  def set_outer_wall(self):
    for y in range(0, self.height):
      for x in range(0, self.width):
        if x == 0 or y == 0 or x == self.width-1 or y == self.height-1:
          self.maze[y][x] = self.WALL
    return self.maze

  def set_inner_wall(self):
    for y in range(2, self.height-1, 2):
      for x in range(2, self.width-1, 2):
        self.maze[y][x] = self.WALL
    return self.maze

  def set_maze_boutaoshi(self):
    self.set_outer_wall()
    self.set_inner_wall()
    for y in range(2, self.height-1, 2):
      for x in range(2, self.width-1, 2):
        while True:
          wall_x = x
          wall_y = y
          if y == 2:
            direction = random.randrange(0, 4)
          else:
            direction = random.randrange(0, 3)
          if direction == 0:
            wall_x += 1
          elif direction == 1:
            wall_y += 1
          elif direction == 2:
            wall_x -= 1
          elif direction == 3:
            wall_y -= 1
          if self.maze[wall_y][wall_x] != self.WALL:
            self.maze[wall_y][wall_x] = self.WALL
            break
    return self.maze

  def set_start_goal(self, start, goal):
    if self.maze[start[1]][start[0]] == self.PATH:
      self.start = start
    if self.maze[goal[1]][goal[0]] == self.PATH:
      self.goal = goal
    return self.maze

  def set_shortest_path(self):
    point = self.goal
    self.maze[point[1]][point[0]] = '*'
    while self.dist[point[1]][point[0]] > 0:
      for x in [[0,-1],[1,0],[0,1],[-1,0]]:
        if self.dist[point[1]][point[0]]-self.dist[point[1]+x[1]][point[0]+x[0]] == 1:
          if self.dist[point[1]][point[0]] > 0:
            self.maze[point[1]+x[1]][point[0]+x[0]] = '*'
            point = [point[0]+x[0],point[1]+x[1]]
    return self.maze

  def print_maze(self):
    self.maze[self.start[1]][self.start[0]] = 'S'
    self.maze[self.goal[1]][self.goal[0]] = 'G'
    for col in self.maze:
      for cell in col:
        if cell == self.WALL:
          print('#', end='')
        elif cell == self.PATH:
          print(' ', end='')
        elif cell == 'S':
          print('S', end='')
        elif cell == 'G':
          print('G', end='')
        elif cell == '*':
          print('*', end='')
      print()

maze1 = Maze(15, 15)
maze1.set_maze_boutaoshi()
maze1.set_start_goal([5, 5], [9, 11])
maze1.set_shortest_path()
maze1.print_maze()

今回は、以下のように出力される。

###############
# # #   # #   #
# # # ### ### #
#*******      #
#*# ###*##### #
#*# #S**    # #
#*########### #
#*****#       #
# ###*##### # #
#   #*****# # #
### ### #*### #
#     # #G#   #
### ### ### # #
#     # #   # #
###############

コメント

タイトルとURLをコピーしました