院卒新人サラリーマンのメモ代わり

備忘としてのメモを記載

数学パズルQ8

def move(num, hist = [], cur_pos = [0, 0])
  hist = [*hist, cur_pos] 
  return 0 if hist.count > num + 1
  return 1 if hist.count == num + 1

  around = [[0, 1], [0, -1], [1, 0], [-1, 0]]

  around.reduce(0) do |acc, ar|
    next_pos = [cur_pos[0] + ar[0], cur_pos[1] + ar[1]]
    hist.include?(next_pos) ? acc : acc + move(num, hist, next_pos)
  end
end

puts move(12)