Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
1
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

@Nabetani

yokohama.rb#70 に出した問題の実装例(ruby)

yokohama.rb#70( https://yokohamarb.doorkeeper.jp/events/45057 )で、いわゆる「どう書く」をやりました。

問題の方は http://nabetani.sakura.ne.jp/yokohamarb/2016.07.ront/

です。
実際のところ、以前出した http://nabetani.sakura.ne.jp/hena/ord5railsontiles/ のリメイクです。

で。

会場で紹介した実装例を。

def solve( src )
  map = [nil]*5+[*?a..?p].each_slice(4).map{ |x| [x, nil ] }.flatten+[nil]*5
  cur=[5+1,5]
  seq=map[cur[0]]
  m={ 1=>5, 5=>1, -1=>-5, -5=>-1 }
  loop do
    case src[cur[0]-5 ]
    when "0"; cur[1] = -m[cur[1]]
    when "1"; cur[1] = m[cur[1]]
    when "2"; cur[1] = cur[1]
    when "3"; return seq
    end
    cur[0]+=cur[1]
    return seq unless map[cur[0]]
    seq += map[cur[0]]
  end
end

def test( src, expected )
  actual = solve( src )
  okay = actual == expected
  p [ okay ? "ok" : "**NG**", src,actual, expected ]
end

test( "0113/1201/2201/2100", "bcgfeabfjnoklpo" ) # 0    
test( "0211/1200/2220/2103", "bfjnokghdc" ) # 30

テストデータの大半は省略。

実装方針としては、

  • 地図は 1次元で持つ。
  • 盤面の外に出たかどうかを、番兵で判断する

というもの。

when "2" の代入文は不要なんだけど、なんとなく書いてみた。

他の方の実装や感想は

などをご覧いただければと思う。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
1
Help us understand the problem. What are the problem?