結果

問題 No.3 ビットすごろく
ユーザー 小指が強い人
提出日時 2015-12-06 13:04:43
言語 Ruby
(3.4.1)
結果
WA  
実行時間 -
コード長 758 bytes
コンパイル時間 85 ms
コンパイル使用メモリ 7,296 KB
実行使用メモリ 13,056 KB
最終ジャッジ日時 2024-09-14 15:17:44
合計ジャッジ時間 4,825 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 16 WA * 17
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:39: warning: ambiguous first argument; put parentheses or a space even after `-' operator
Syntax OK

ソースコード

diff #

def process(count, ni)
    if ni == $n
        $min = count if count < $min
        return
    end
    if count >= $n / 2 || count >= $min
        return
    end
    front = ni + $bit[ni]
    back = ni - $bit[ni]
    if front <= $n && $a[ni] == 0
        $a[ni] = 1
        process(count + 1, front)
    elsif $a[ni] == 0
        $a[ni] = 1
    end
    if back > 0 && $a[ni] <= 1
        $a[ni] = 2
        process(count + 1, back)
    elsif $a[ni] <= 1
        $a[ni] = 2
    end
    return
end
$n = gets.to_i
$a = Array.new($n + 1, 0)
$bit = Array.new($n + 1, 0)
$min = 99999999
($n + 1).times do |i| 
    s = i.to_s(2)
    s.each_char do |c|
        $bit[i] += 1 if c == "1"
    end
end
process(1, 1)
if $min < 99999999
    puts $min
else
    puts -1
end
0