結果

問題 No.3 ビットすごろく
ユーザー tana_twtrtana_twtr
提出日時 2016-10-30 15:51:29
言語 Ruby
(3.3.0)
結果
WA  
実行時間 -
コード長 1,089 bytes
コンパイル時間 287 ms
コンパイル使用メモリ 7,424 KB
実行使用メモリ 19,872 KB
最終ジャッジ日時 2024-05-03 19:48:59
合計ジャッジ時間 7,168 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 84 ms
12,288 KB
testcase_03 AC 89 ms
12,288 KB
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

def main
    
    n = STDIN.readline.chomp.to_i
    table_forward, table_back = init_table(n)
    count = sugoroku(n, n, 0, table_forward, table_back)
    puts count < n ? count : -1

end

def init_table(n)

    table_forward = []
    table_back = []

    1.upto(n) do |i|

        table_forward[i] = i + bitcount(i)
        table_back[i] = i - bitcount(i)

    end

    [table_forward, table_back]

end

def sugoroku(n, goal, count, table_forward, table_back)

    ret = n

    if count >= n
        return ret
    elsif goal == 1
        return count
    end

    1.upto(goal - 1) do |i|
        if table_forward[i] == goal
            ret_tmp = sugoroku(n, i, count + 1, table_forward, table_back)
            ret = ret_tmp if ret_tmp < ret
        end
    end

    n.downto(goal + 1) do |i|
        if table_back[i] == goal
            ret_tmp = sugoroku(n, i, count + 1, table_forward, table_back)
            ret = ret_tmp if ret_tmp < ret
        end
    end

    ret

end

def bitcount(n)

    count = 0
    n.bit_length.times {|i| count += 1 if n[i] == 1}
    count

end

main()

0