Help us understand the problem. What is going on with this article?

# 迷路を作るアルゴリズムを実装する

More than 3 years have passed since last update.

## アルゴリズムの概要

さらに2と3のマスを選択する。

この操作をすべてのマスが0になるまで繰り返す。

## 実装

```import random
import numpy as np
from itertools import product

news = ['n', 'e', 'w', 's']
SPACE = '　'
WALL = '■'
START = 'Ｓ'
GOAL = 'Ｇ'

def print_maze(maze):
"""
迷路を出力
"""
with open('maze.txt', 'w') as f:
for row in maze:
f.write(''.join(row) + '\n')

# 迷路のサイズを指定
m, n = 30, 50

memo = np.array([i for i in range(n * m)])
memo = memo.reshape(m, n)

# 迷路を初期化
maze = [[SPACE for _ in range(2 * n + 1)] for _ in range(2 * m + 1)]
maze[1][1] = START
maze[2 * m - 1][2 * n - 1] = GOAL
for i, j in product(range(2 * m + 1), range(2 * n + 1)):
if i % 2 == 0 or j % 2 == 0:
maze[i][j] = WALL

while (memo != 0).any():
x1 = random.choice(range(m))
y1 = random.choice(range(n))
direction = random.choice(news)

if direction == 'e':
x2, y2 = x1, y1 + 1
elif direction == 'w':
x2, y2 = x1, y1 - 1
elif direction == 'n':
x2, y2 = x1 - 1, y1
elif direction == 's':
x2, y2 = x1 + 1, y1

# 範囲外の場合はcontinue
if (x2 < 0) or (x2 >= m) or (y2 < 0) or (y2 >= n):
continue

if memo[x1, y1] != memo[x2, y2]:
tmp_min = min(memo[x1, y1], memo[x2, y2])
tmp_max = max(memo[x1, y1], memo[x2, y2])

# メモの更新
memo[memo == tmp_max] = tmp_min

# 壁を壊す
maze[x1 + x2 + 1][y1 + y2 + 1] = SPACE

print_maze(maze)
```
Tech FunはITの力で世界を豊かにする総合サービス企業です。 IT研修スクール「TechFun.jp（https://techfun.jp/）」、eラーニングプラットフォーム「StudySmile（https://studysmile.com/）」のほか、ミャンマーオフショア開発、スマートフォンアプリ開発、Webシステム開発、SIサービスを展開しています。
https://www.techfun.co.jp/
Why not register and get more from Qiita?
1. We will deliver articles that match you
By following users and tags, you can catch up information on technical fields that you are interested in as a whole
2. you can read useful information later efficiently
By "stocking" the articles you like, you can search right away