結果

問題 No.3 ビットすごろく
ユーザー kohaku_kohakukohaku_kohaku
提出日時 2016-12-12 00:15:33
言語 Java21
(openjdk 21)
結果
AC  
実行時間 138 ms / 5,000 ms
コード長 947 bytes
コンパイル時間 2,153 ms
コンパイル使用メモリ 74,852 KB
実行使用メモリ 56,284 KB
最終ジャッジ日時 2023-09-14 00:14:39
合計ジャッジ時間 8,083 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 127 ms
56,148 KB
testcase_01 AC 125 ms
55,760 KB
testcase_02 AC 125 ms
55,960 KB
testcase_03 AC 129 ms
55,868 KB
testcase_04 AC 127 ms
55,856 KB
testcase_05 AC 128 ms
55,912 KB
testcase_06 AC 129 ms
55,896 KB
testcase_07 AC 128 ms
56,140 KB
testcase_08 AC 129 ms
55,452 KB
testcase_09 AC 131 ms
55,892 KB
testcase_10 AC 135 ms
54,032 KB
testcase_11 AC 129 ms
55,768 KB
testcase_12 AC 129 ms
55,540 KB
testcase_13 AC 128 ms
55,568 KB
testcase_14 AC 137 ms
55,900 KB
testcase_15 AC 135 ms
54,404 KB
testcase_16 AC 136 ms
55,964 KB
testcase_17 AC 137 ms
55,788 KB
testcase_18 AC 129 ms
55,892 KB
testcase_19 AC 136 ms
55,884 KB
testcase_20 AC 127 ms
56,136 KB
testcase_21 AC 125 ms
56,284 KB
testcase_22 AC 136 ms
55,764 KB
testcase_23 AC 135 ms
55,900 KB
testcase_24 AC 136 ms
55,780 KB
testcase_25 AC 138 ms
55,812 KB
testcase_26 AC 127 ms
55,872 KB
testcase_27 AC 127 ms
55,948 KB
testcase_28 AC 134 ms
55,940 KB
testcase_29 AC 128 ms
56,088 KB
testcase_30 AC 124 ms
56,144 KB
testcase_31 AC 124 ms
54,192 KB
testcase_32 AC 129 ms
55,768 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;
        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(c[N]==Integer.MAX_VALUE)c[N]=-1;
        System.out.println(c[N]);
    }
}
0