結果

問題 No.3 ビットすごろく
ユーザー tana_twtrtana_twtr
提出日時 2016-10-30 17:57:30
言語 Ruby
(3.3.0)
結果
WA  
実行時間 -
コード長 1,189 bytes
コンパイル時間 32 ms
コンパイル使用メモリ 7,552 KB
実行使用メモリ 19,456 KB
最終ジャッジ日時 2024-05-03 19:54:42
合計ジャッジ時間 14,049 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 90 ms
12,032 KB
testcase_01 AC 97 ms
12,160 KB
testcase_02 AC 91 ms
12,032 KB
testcase_03 AC 99 ms
12,032 KB
testcase_04 WA -
testcase_05 AC 104 ms
12,032 KB
testcase_06 AC 96 ms
12,032 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 TLE -
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, 1, 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, visited)

    ret = n

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

    visited[goal] = true

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

    n.downto(goal + 1) do |i|
        if visited[i] == nil && table_back[i] == goal
            ret_tmp = sugoroku(n, i, count + 1, table_forward, table_back, visited)
            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