結果
| 問題 | No.3 ビットすごろく | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2019-07-05 10:17:03 | 
| 言語 | Ruby (3.4.1) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 160 ms / 5,000 ms | 
| コード長 | 509 bytes | 
| コンパイル時間 | 80 ms | 
| コンパイル使用メモリ | 7,552 KB | 
| 実行使用メモリ | 12,672 KB | 
| 最終ジャッジ日時 | 2024-07-01 09:24:11 | 
| 合計ジャッジ時間 | 5,088 ms | 
| ジャッジサーバーID (参考情報) | judge2 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 33 | 
コンパイルメッセージ
Main.rb:22: warning: mismatched indentations at 'end' with 'if' at 17 Main.rb:29: warning: ambiguous first argument; put parentheses or a space even after `-' operator Syntax OK
ソースコード
N = gets.to_i
INF = 10**10
dp = Array.new(N+1){ INF }
dp[0], dp[1] = 0, 1
def move(dp, from)
  dist = from.to_s(2).count('1')
	# ++
	if from + dist <= N
    if dp[from + dist] > dp[from] + 1
      dp[from + dist] = dp[from] + 1
      dp = move(dp, from + dist)
    end
	end
	# --
	if from - dist >= 0
    if dp[from - dist] > dp[from] + 1
      dp[from - dist] = dp[from] + 1
      dp = move(dp, from - dist)
    end
  end
  return dp
end
dp = move(dp, 1)
if dp[-1] == INF
  puts -1
else
  puts dp[-1]
end
            
            
            
        