0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

プログラマ脳を鍛える数学パズルQ08 優秀な掃除ロボット

Last updated at Posted at 2020-04-23

問題概要

前後左右に動く掃除ロボットが12ステップで移動できる経路のパターン数を求める。ただし同じ場所は2回通らないこととする。

Code

深さ優先探索。再帰を使って書く。

N = 12 #動く回数

def dfs(path):
    if len(path) > N:
        return 1
    cnt = 0 #パターン数
    for d in [(0,1), (0,-1), (1,0), (-1,0)]:
        next = path[-1][0] + d[0], path[-1][1] + d[1]
        if next not in path:
            cnt += dfs(path + [next])
    return cnt

print(dfs([(0, 0)])) #初期位置を(0, 0)とする

ToDo

Pythonの再帰は遅いらしい。スタックを使って書いたほうが早いか?

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?