forked from y-tetsu/math_puzzle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
q59.py
51 lines (38 loc) · 1.09 KB
/
q59.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Q59
"""
N = 8
CIRCLE = [i + 1 for i in range(N)]
LIMIT = 98
def oni_run(num, pos, circle, total):
"""
鬼が走る
"""
if total >= LIMIT:
return LIMIT
if num == 0 and is_reverse(circle, CIRCLE):
print("reverse", total, circle)
return total
min_dist = LIMIT
for next_pos in range(N):
next_circle = circle[:]
dist = next_pos - pos + N if next_pos >= pos else next_pos - pos + N * 2
next_circle[next_pos], next_num = num, circle[next_pos]
next_total = total + dist
min_dist = min(oni_run(next_num, next_pos, next_circle, next_total), min_dist)
return min_dist
def is_reverse(arr1, arr2):
"""
逆順かどうかをチェック
"""
num = len(arr1)
reverse_arr2 = arr2[::-1]
offset = reverse_arr2.index(1) - arr1.index(1)
for index, value in enumerate(arr1):
if value != reverse_arr2[(index + offset) % num]:
return False
return True
if __name__ == '__main__':
print("result", oni_run(0, 0, CIRCLE, 0))