結果

問題 No.3 ビットすごろく
ユーザー SilverBullet _RyeSilverBullet _Rye
提出日時 2023-12-02 13:55:14
言語 Java21
(openjdk 21)
結果
AC  
実行時間 170 ms / 5,000 ms
コード長 763 bytes
コンパイル時間 3,859 ms
コンパイル使用メモリ 75,880 KB
実行使用メモリ 58,080 KB
最終ジャッジ日時 2023-12-02 13:55:28
合計ジャッジ時間 8,316 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 131 ms
57,644 KB
testcase_01 AC 133 ms
57,612 KB
testcase_02 AC 132 ms
57,552 KB
testcase_03 AC 133 ms
57,664 KB
testcase_04 AC 132 ms
57,568 KB
testcase_05 AC 133 ms
57,584 KB
testcase_06 AC 133 ms
57,660 KB
testcase_07 AC 166 ms
57,568 KB
testcase_08 AC 139 ms
57,568 KB
testcase_09 AC 141 ms
57,660 KB
testcase_10 AC 145 ms
58,072 KB
testcase_11 AC 136 ms
57,676 KB
testcase_12 AC 136 ms
57,708 KB
testcase_13 AC 133 ms
57,644 KB
testcase_14 AC 140 ms
57,644 KB
testcase_15 AC 169 ms
57,640 KB
testcase_16 AC 146 ms
58,080 KB
testcase_17 AC 145 ms
57,600 KB
testcase_18 AC 139 ms
57,644 KB
testcase_19 AC 146 ms
57,676 KB
testcase_20 AC 141 ms
57,544 KB
testcase_21 AC 141 ms
57,548 KB
testcase_22 AC 145 ms
57,680 KB
testcase_23 AC 170 ms
57,676 KB
testcase_24 AC 138 ms
57,656 KB
testcase_25 AC 144 ms
57,608 KB
testcase_26 AC 135 ms
57,644 KB
testcase_27 AC 137 ms
57,648 KB
testcase_28 AC 143 ms
57,644 KB
testcase_29 AC 138 ms
57,548 KB
testcase_30 AC 132 ms
57,560 KB
testcase_31 AC 161 ms
57,644 KB
testcase_32 AC 135 ms
57,576 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

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