forked from y-tetsu/math_puzzle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
q65.py
76 lines (63 loc) · 1.87 KB
/
q65.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python
"""
Q65
"""
N = 12
START = [i for i in range(1, N)] + [0]
GOAL = [0] + [i for i in range(2, N)] + [1]
MEMO_S = {}
MEMO_G = {}
def throw_ball(person):
"""
ボールを投げる
"""
ret = []
for num, _ in enumerate(person):
if num < len(person)//2:
for i in range(3):
target = num + (len(person)//2-1) + i
if len(person)//2 <= target < len(person):
if person[target] == 0:
tmp = [i for i in person]
tmp[num], tmp[target] = tmp[target], tmp[num]
ret.append(tmp)
else:
for i in range(3):
target = num - (len(person)//2+1) + i
if 0 <= target < len(person)//2:
if person[target] == 0:
tmp = [i for i in person]
tmp[num], tmp[target] = tmp[target], tmp[num]
ret.append(tmp)
return ret
def search(start, goal, count):
"""
探索
"""
ret_start = []
ret_goal = []
# スタート側からの探索
count += 1
for i in start:
for j in throw_ball(i):
if j not in ret_start:
if tuple(j) not in MEMO_S:
MEMO_S[tuple(j)] = 1
ret_start.append(j)
for i in ret_start:
if i in goal:
return count
# ゴール側からの探索
count += 1
for i in goal:
for j in throw_ball(i):
if j not in ret_goal:
if tuple(j) not in MEMO_G:
MEMO_G[tuple(j)] = 1
ret_goal.append(j)
for i in ret_start:
if i in ret_goal:
return count
return search(ret_start, ret_goal, count)
if __name__ == '__main__':
print("result =", search([START], [GOAL], 0))