0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

第30回オフラインリアルタイムどう書くの解答

Posted at

第30回 オフラインリアルタイムどう書くの問題を家で解きました。

問題のURL( http://nabetani.sakura.ne.jp/hena/ord30taxi/ )

45分ほど
経路の終点でちょうど値段が上がるケースは問題上ありえなかったので、ひとまずは無視しました。

都市のデータの持ち方はもっと良い方法を考えたほうが良かった気がする。

def process(path)
  (0...(path.length-1)).map { |index|
    {
      'AB' => [ 1090, 0, ],
      'AC' => [ 180, 0, ],
      'AD' => [ 0, 540, ],
      'BC' => [ 960, 0, ],
      'BG' => [ 1270, 0, ],
      'CD' => [ 0, 400, ],
      'CF' => [ 200, 0, ],
      'DE' => [ 0, 720, ],
      'DF' => [ 0, 510, ],
      'EG' => [ 0, 1050, ],
      'FG' => [ 0, 230, ],
    }[path[index, 2].chars.sort.join]
  }.inject( {
              'A' => [ 400, 995, ],
              'B' => [ 400, 995, ],
              'C' => [ 400, 995, ],
              'D' => [ 350, 845, ],
              'E' => [ 350, 845, ],
              'F' => [ 350, 845, ],
              'G' => [ 350, 845, ],
            }[path[0]] ) { |result, distance|
    distance.zip( [ 60, 50, ] ).each { |edge_distance, price|
      result[1] = result[1] - edge_distance
      while result[1] < 0
        result = result.zip( [price, 200] ).map { |left, right| left + right }
      end
    }
    result
  }[0].to_s
end

def test(input, answer)
  result = process(input)
  if result != answer then
    puts 'test error: input = %s, result = %s, but answer = %s'%[ input, result, answer ]
  end
end

test( "ADFC", "510" )
# テストケース略
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?