結果

問題 No.3 ビットすごろく
ユーザー TakashinaInHubTakashinaInHub
提出日時 2016-01-30 13:29:24
言語 Ruby
(3.3.0)
結果
WA  
実行時間 -
コード長 1,288 bytes
コンパイル時間 381 ms
コンパイル使用メモリ 11,944 KB
実行使用メモリ 47,472 KB
最終ジャッジ日時 2023-10-21 17:54:55
合計ジャッジ時間 7,529 ms
ジャッジサーバーID
(参考情報)
judge14 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 TLE -
testcase_04 -- -
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 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:65: warning: ambiguous first argument; put parentheses or a space even after `-' operator
Syntax OK

ソースコード

diff #

def steps(n)
    i = 0
    while n > 0
        i += 1 if n%2 == 1
        n >>=  1
    end
    i
end

def mapping(n, p)
    dis = steps(p)
    fowdis = p + dis
    backdis = p - dis
    map = Array.new(2, -1)
    map[0] = fowdis if fowdis <= n
    map[1] = backdis if 1 <= backdis
    map
end

def scanroot(map_list, n, goal, fp=Array.new())
    puts "0: Start #{n}"
    if fp.include?(n) or n == -1
        fp << -1
        puts "1: #{fp}" if false
        return fp
    elsif n == goal
        fp << n
        puts "2: #{fp}" if false
        return fp
    end

    fp << n
    2.times { |i|
        tfp = scanroot(map_list, map_list[n-1][i], goal, Marshal.load(Marshal.dump(fp)))
        if tfp[-1] == -1
            puts "4: #{fp}" if false
            next
        end
        if tfp[-1] == goal and (fp[-1] != goal or fp.length >= tfp.length)
            puts "5: #{fp}" if false
            fp = tfp
            puts "6: #{fp}" if false
        end
    }
    
    if fp[-1] != goal
        fp << -1
        puts "7: #{fp}" if false
    end
    return fp
end

n = gets.to_i
map_list = Array.new(n)
n.times { |i|
  map_list[i] = mapping(n, i+1)
}

footprint = Array.new(n, -1)
footprint = scanroot(map_list, 1, n)

if footprint[-1] == n
    puts footprint.length
else
    puts -1
end
0