結果

問題 No.3 ビットすごろく
ユーザー SilverBullet _RyeSilverBullet _Rye
提出日時 2023-12-02 13:50:32
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 872 bytes
コンパイル時間 8,007 ms
コンパイル使用メモリ 85,056 KB
実行使用メモリ 116,456 KB
最終ジャッジ日時 2023-12-02 13:50:56
合計ジャッジ時間 18,469 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 129 ms
57,828 KB
testcase_01 AC 131 ms
57,820 KB
testcase_02 AC 117 ms
56,688 KB
testcase_03 AC 269 ms
63,272 KB
testcase_04 AC 174 ms
60,008 KB
testcase_05 AC 743 ms
71,700 KB
testcase_06 AC 254 ms
63,080 KB
testcase_07 AC 225 ms
61,332 KB
testcase_08 AC 368 ms
69,632 KB
testcase_09 AC 4,944 ms
116,456 KB
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 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        Set<Integer> fin = new HashSet<>();
        Queue<Integer> target = new ArrayDeque<>();
        Queue<Integer> cntQ = new ArrayDeque<>();
        target.add(1);
        cntQ.add(1);
        while (!target.isEmpty()){
            int now = target.poll();
            if(now==n){
                System.out.println(cntQ.poll());
                return;
            }
            fin.add(now);
            int bit = Integer.bitCount(now);
            int cnt = cntQ.poll();
            if(now-bit>=1 && !fin.contains(now-bit)){target.add(now-bit);cntQ.add(cnt+1);}
            if(now+bit<=n && !fin.contains(now+bit)){target.add(now+bit);cntQ.add(cnt+1);}
        }
        System.out.println(-1);
    }
}
0