結果

問題 No.3 ビットすごろく
ユーザー gemmaro
提出日時 2020-05-22 11:36:47
言語 Ruby
(3.4.1)
結果
WA  
実行時間 -
コード長 1,060 bytes
コンパイル時間 106 ms
コンパイル使用メモリ 7,552 KB
実行使用メモリ 13,312 KB
最終ジャッジ日時 2024-10-04 06:44:11
合計ジャッジ時間 3,983 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 32 WA * 1
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

# frozen_string_literal: true

# rubocop:todo Metrics/PerceivedComplexity
# rubocop:todo Metrics/CyclomaticComplexity
# rubocop:todo Metrics/AbcSize
def solve # rubocop:todo Metrics/MethodLength
  return 0 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
0