ヤマカサのプログラミング勉強日記

プログラミングに関する日記とどうでもよい雑記からなるブログです。

AtCoder 300点の問題を Ruby で解き直す その1

ABC 051 C - Back and Forth

最短経路の問題。

一周目の移動距離はマンハッタン距離に等しい。

二周目は  +8 される。

コード

sx, sy, tx, ty = gets.chop.split.map(&:to_i)

dx = tx - sx
dy = ty - sy

# 1 周目
ans = ""
dx.times do
  ans += "R"
end

dy.times do
  ans += "U"
end

dx.times do
  ans += "L"
end

dy.times do
  ans += "D"
end

# 2 周目
ans += "D"

(dx + 1).times do
  ans += "R"
end

(dy + 1).times do
  ans += "U"
end

ans += "L"

ans += "U"

(dx + 1).times do
  ans += "L"
end

(dy + 1).times do
  ans += "D"
end

ans += "R"
puts ans