-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathboukesi.rb
57 lines (43 loc) · 829 Bytes
/
boukesi.rb
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
@m = {}
def search(board, depth=0)
is_player = depth % 2 == 0
if board.empty?
return is_player
end
# p board
if @m[board]
return !(@m[board] ^ is_player)
end
board.each_with_index do |v, i|
v.times do |s|
1.upto(v) do |l|
if s + l > v
next
end
b = board.dup
b.delete_at(i)
b << s if s > 0
rl = v - s - l
b << rl if rl > 0
r = search(b, depth+1)
if r && depth == 0
puts "#{i+1} #{s}-#{l}"
end
if r == is_player
@m[board] = true
return is_player
end
end
end
end
@m[board] = false
return !is_player
end
#board = [1,2,3,4,5]
#search(board)
#board = [2,1,4,5]
#search(board)
#board = [1,4,3]
#search(board)
board = [1,2,1]
search(board)