8. N,
@T
=
gets.split.map(&:to_i)
@t=0
@plates
=
[
N.downto(1).to_a,[],[]
]
def
show
(0..2).each{|i|
if
@plates[i].size
==
0
puts
"-‐"
else
puts
@plates[i].join("
")
end
}
end
def
move_one(n,
from,
to)
plate
=
@plates[from].pop
@plates[to].push(plate)
@t
+=
1
if
@t
==
@T
show
exit
end
end
def
hanoi(n,
from,
to,
other)
if
n
==
0
return
end
hanoi(n-‐1,
from,
other,
to)
move_one(n,
from,
to)
hanoi(n-‐1,
other,
to,
from)
end
hanoi(N,
0,
2,
1)
12. コード
def
hanoi(n,
from,
to,
other)
if
n
==
0
return
end
if
@T
>=
@t
+
(2**n)-‐1
plates
=
@plates[from].pop(n)
@plates[to].push(*plates)
@t
+=
(2**n
-‐
1)
show
if
@t
==
@T
return
end
…