結果

問題 No.3 ビットすごろく
ユーザー kohaku_kohakukohaku_kohaku
提出日時 2016-12-12 00:39:48
言語 Java21
(openjdk 21)
結果
AC  
実行時間 148 ms / 5,000 ms
コード長 1,017 bytes
コンパイル時間 2,157 ms
コンパイル使用メモリ 75,172 KB
実行使用メモリ 57,708 KB
最終ジャッジ日時 2023-09-14 00:15:09
合計ジャッジ時間 8,143 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 138 ms
57,708 KB
testcase_01 AC 132 ms
55,776 KB
testcase_02 AC 133 ms
55,728 KB
testcase_03 AC 133 ms
55,776 KB
testcase_04 AC 130 ms
55,780 KB
testcase_05 AC 131 ms
55,936 KB
testcase_06 AC 130 ms
55,924 KB
testcase_07 AC 130 ms
56,180 KB
testcase_08 AC 133 ms
55,688 KB
testcase_09 AC 133 ms
55,916 KB
testcase_10 AC 136 ms
55,764 KB
testcase_11 AC 133 ms
55,568 KB
testcase_12 AC 132 ms
55,564 KB
testcase_13 AC 133 ms
55,784 KB
testcase_14 AC 139 ms
55,820 KB
testcase_15 AC 137 ms
55,840 KB
testcase_16 AC 148 ms
55,776 KB
testcase_17 AC 137 ms
55,960 KB
testcase_18 AC 130 ms
55,760 KB
testcase_19 AC 146 ms
55,760 KB
testcase_20 AC 131 ms
55,716 KB
testcase_21 AC 128 ms
55,696 KB
testcase_22 AC 143 ms
55,948 KB
testcase_23 AC 142 ms
55,728 KB
testcase_24 AC 139 ms
55,960 KB
testcase_25 AC 141 ms
55,784 KB
testcase_26 AC 129 ms
55,544 KB
testcase_27 AC 135 ms
55,764 KB
testcase_28 AC 136 ms
55,796 KB
testcase_29 AC 131 ms
55,852 KB
testcase_30 AC 125 ms
55,748 KB
testcase_31 AC 129 ms
56,372 KB
testcase_32 AC 132 ms
56,184 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        boolean [] boo = new boolean [N+1];
        int [] c = new int [N+1];
        for(int i=2; i<=N; i++){
            c[i]=Integer.MAX_VALUE;
        }
        c[1]=1;boo[1]=true;
        Queue<Integer> q = new LinkedList<>();
        q.add(1);
        while(!q.isEmpty()){
            int t = q.poll();
            int p = Integer.bitCount(t);
            int b = t-p;
            int f = t+p;
            if(b>0){
                c[b]=Math.min(c[b],c[t]+1);
                if(boo[b]==false)q.add(b);
                boo[b]=true;
            }
            if(f<=N){
                c[f]=Math.min(c[f],c[t]+1);
                if(boo[f]==false)q.add(f);
                boo[f]=true;
            }
            if(boo[N]){
                System.out.println(c[N]);
                return;
            }
        }
        System.out.println(-1);
    }
}
0