LoginSignup
0
0

More than 5 years have passed since last update.

第12回オフラインリアルタイムどう書くの参考問題。ruby による実装

Posted at

オフラインリアルタイムどう書く
http://atnd.org/events/40389
の、参考問題(道なりの亀)
http://nabetani.sakura.ne.jp/hena/ord12aloroturtle/
の実装例。ruby。

他の言語などの解答例は
http://qiita.com/Nabetani/items/1de39df381dfeee305ab
から辿れます。

で。
こんな感じ。

def at( a, x, y )
  return nil unless (0...a.size).include?( y )
  line=a[y]
  (0...line.size).include?( x ) ? line[x] : nil
end

def create_graph
  names=[(?A..?Z), (?a..?z), (?0..?7), %w( g f e )].map{ |i| i.to_a }.flatten
  f=( [11]*3+[3]*10 ).map{ |w| names.shift(w) }

  (0..11).each_with_object({}) { |y,r|
    0.upto(y<3 ? 10 : 2) do |x|
      r[ at( f, x, y )] = 4.times.map{ |i|
        at( f, x+[-1,0,1,0][i], y+[0,-1,0,1][i] )
      }
    end
  }.tap{ |r|
    %w( e7 f6  g5 ).each do |c|
      r[c[0]][3]=c[1]
    end
  }
end

def solve( src )
  graph=create_graph()
  pos = [ ?A, graph[?A].index( ?B ) ]
  src.chars.each_with_object( "A" ) do |c,r|
    turn={"L"=>3, "R"=>1}[c]
    if turn
      pos[1] += turn
    else
      c.to_i(16).times do 
        np=graph[pos[0]][pos[1]&3]
        return r+"?" unless graph[np]
        pos = [ np, graph[ np ].index( pos[0] )+2 ]
        r.concat np
      end
    end
  end
end

DATA.each_line do |line|
  num, src, exp = line.split( /\s+/ )
  act = solve( src )
  ok = act==exp ? "ok" : "***NG***"
  puts [ ok, src, act, exp ].join( " " )
end

__END__
0 2RcL3LL22 ABCNYjmpsvy147edcbcdef
1 L3R4L5RR5R3L5 A?
40  6LR2R1LR5LRLRL484L63  ABCDEFGHITe741yxw?

いつもどおり、テストデータの大半を省略。

テストを数えなくても 40行。
最初に書いた時はもっと簡単なような気がしていたんだけどなぁ。

作戦としては、基本的に直交座標系である、というものと、基本的にグラフである、という物があると思う。
これは後者。

地図の歪み具合に応じて例外的な処理をしなければならないわけだけど、それは地図製作の時点で済ましている。移動の時は普通にたどればよい。
とはいえ、solve の最後に end が4つもあるのはいただけない。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0