http://nabetani.sakura.ne.jp/hena/ord5railsontiles/
The problem is finding a route that goes out of range when moving along the rails of a given tile.
Ruby
module RailsOnTiles
Tiles = [[2, 3, 0, 1], [1, 0, 3, 2], [3, 2, 1, 0]]
Dirs = [[0, -1], [1, 0], [0, 1], [-1, 0]]
module_function
def go(input)
field = input.chars.map(&:to_i).each_slice(3).to_a
solve(field).map { |i| ("A".."I").to_a[i] }.join
end
def solve(field, x=1, y=-1, dir=2, route=[])
dx, dy = Dirs[dir]
x += dx
y += dy
return route if x < 0 || x > 2 || y < 0 || y > 2
rev = Tiles[0][dir]
nxt = Tiles[field[y][x]][rev]
tile_num = y * 3 + x
solve(field, x, y, nxt, route + [tile_num])
end
end
if __FILE__ == $0
require 'minitest/autorun'
describe 'RailsOnTiles' do
[
["101221102", "BEDGHIFEH"],
["000000000", "BEH"],
["111111111", "BCF"],
["222222222", "BAD"],
["000211112", "BEFIHEDGH"],
["221011102", "BADGHIFEBCF"],
["201100112", "BEHIFCBADEF"],
["000111222", "BEFIH"],
["012012012", "BC"],
["201120111", "BEDABCFI"],
["220111122", "BADEHGD"],
["221011022", "BADG"],
["111000112", "BCFIHEBA"],
["001211001", "BEFI"],
["111222012", "BCFEHIF"],
["220111211", "BADEHI"],
["211212212", "BCFEBAD"],
["002112210", "BEFC"],
["001010221", "BEF"],
["100211002", "BEFIHG"],
["201212121", "BEFCBAD"]
].each do |input, expect|
it input do
assert_equal RailsOnTiles.go(input), expect
end
end
end
end
field
represents the arrangement of tiles, and the tile type is stored in Tiles
. As for the direction type, 0,1,2,3 represent the upper right lower left respectively. If you go down from the previous tile, the next tile will "reverse" so that it comes from the top, so it goes into rev
. After that, it recurses until it goes out of field
and returns the result.
In addition, since the route route
represents the position of the tile with a number, it is converted to the alphabet at the end.
Recommended Posts