#! ruby # yukicoder My Practice # author: Leonardone @ NEETSDKASU ############################################################ def gs() gets.chomp end def gi() gets.to_i end def gf() gets.to_f end def gss() gs.split end def gis() gss.map(&:to_i) end def gfs() gss.map(&:to_f) end def nmapf(n,f) n.times.map{ __send__ f } end def ngs(n) nmapf n,:gs end def ngi(n) nmapf n,:gi end def ngss(n) nmapf n,:gss end def ngis(n) nmapf n,:gis end def arr2d(h,w,v=0) h.times.map{[v] * w} end def for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end def nsum(n) n * (n + 1) / 2 end def vcount(d,r=Hash.new(0)) d.inject(r){|r,e| r[e]+=1;r} end ############################################################ t = 0 f = arr2d(60,60) x = 30 y = 30 dx = 1 dy = 0 UNKNOWN_CELL = 0 WALL_CELL = 1 SPACE_CELL = 2 f[y][x] = SPACE_CELL def rot(dx, dy) [-dy, dx] end def revrot(dx, dy) [dy, -dx] end PERF = "FRBL" def sk(f, x, y, dx, dy) a = [["X",99]] 4.times do |i| tx, ty = x, y rx, ry = rot(dx, dy) lx, ly = revrot(dx, dy) loop { tx += dx ty += dy if f[ty][tx] == WALL_CELL break end if f[ty + ry][tx + rx] == UNKNOWN_CELL || f[ty + ly][tx + lx] == UNKNOWN_CELL a << [PERF[i], (tx - x).abs + (ty - y).abs] break end } dx, dy = rot(dx, dy) end a.min_by{|_,d|d} end loop { s = STDIN.gets break if s[0] == "M" if s.size > 2 STDOUT.puts "F" else d = s.to_i nx = "X" if f[y + dy][x + dx] == UNKNOWN_CELL (1..d).each do |i| f[y + dy * i][x + dx * i] = SPACE_CELL end f[y + dy * d.succ][x + dx * d.succ] = WALL_CELL end nx, ny = rot(dx, dy) if f[y + ny][x + nx] == UNKNOWN_CELL nx = "R" else nx, ny = revrot(dx, dy) if f[y + ny][x + nx] == UNKNOWN_CELL nx = "L" else nx, ny = revrot(nx, ny) if f[y + ny][x + xy] == UNKNOWN_CELL nx = "L" else nx, _ = sk(f, x, y, dx, dy) end end end case nx when "F" x += dx y += dy when "R" dx, dy = rot(dx, dy) when "L" dx, dy = revrot(dx, dy) when "B" x -= dx y -= dy else puts PREF[rand(4)] end end STDOUT.flush }