結果
問題 | No.3 ビットすごろく |
ユーザー |
|
提出日時 | 2020-05-22 11:38:15 |
言語 | Ruby (3.4.1) |
結果 |
AC
|
実行時間 | 106 ms / 5,000 ms |
コード長 | 1,060 bytes |
コンパイル時間 | 257 ms |
コンパイル使用メモリ | 7,424 KB |
実行使用メモリ | 13,440 KB |
最終ジャッジ日時 | 2024-07-01 09:48:07 |
合計ジャッジ時間 | 4,186 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 33 |
コンパイルメッセージ
Syntax OK
ソースコード
# frozen_string_literal: true # rubocop:todo Metrics/PerceivedComplexity # rubocop:todo Metrics/CyclomaticComplexity # rubocop:todo Metrics/AbcSize def solve # rubocop:todo Metrics/MethodLength return 1 if N == 1 positions = [0] squares = Array.new(N, nil) squares[0] = 0 step = 1 N.times do new_positions = [] positions.each do |position| bit_count = BIT_COUNTS[position + 1] left = position - bit_count right = position + bit_count return step + 1 if right == N - 1 if left >= 0 && (squares[left].nil? || squares[left] > step) squares[left] = step new_positions << left end if right <= N - 1 && (squares[right].nil? || squares[right] > step) squares[right] = step new_positions << right end end positions = new_positions step += 1 end -1 end # rubocop:enable Metrics/AbcSize # rubocop:enable Metrics/CyclomaticComplexity # rubocop:enable Metrics/PerceivedComplexity N = gets.to_i BIT_COUNTS = (0..N).map { _1.digits(2).sum } puts solve