結果

問題 No.3 ビットすごろく
ユーザー kohaku_kohakukohaku_kohaku
提出日時 2016-12-12 00:39:11
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,005 bytes
コンパイル時間 4,161 ms
コンパイル使用メモリ 74,100 KB
実行使用メモリ 56,428 KB
最終ジャッジ日時 2023-08-19 11:45:46
合計ジャッジ時間 8,314 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 121 ms
55,296 KB
testcase_01 AC 122 ms
55,828 KB
testcase_02 AC 123 ms
55,868 KB
testcase_03 AC 125 ms
55,720 KB
testcase_04 AC 124 ms
55,872 KB
testcase_05 AC 123 ms
55,376 KB
testcase_06 AC 124 ms
55,824 KB
testcase_07 AC 125 ms
56,024 KB
testcase_08 AC 124 ms
55,656 KB
testcase_09 AC 126 ms
55,904 KB
testcase_10 AC 130 ms
55,916 KB
testcase_11 AC 124 ms
55,632 KB
testcase_12 AC 123 ms
55,748 KB
testcase_13 AC 121 ms
55,844 KB
testcase_14 AC 131 ms
56,032 KB
testcase_15 AC 130 ms
55,392 KB
testcase_16 AC 132 ms
55,688 KB
testcase_17 AC 134 ms
55,896 KB
testcase_18 AC 126 ms
56,112 KB
testcase_19 AC 136 ms
55,800 KB
testcase_20 AC 122 ms
55,844 KB
testcase_21 AC 121 ms
55,576 KB
testcase_22 AC 131 ms
56,104 KB
testcase_23 AC 131 ms
55,604 KB
testcase_24 AC 131 ms
55,824 KB
testcase_25 AC 132 ms
55,832 KB
testcase_26 WA -
testcase_27 AC 122 ms
55,824 KB
testcase_28 AC 129 ms
56,024 KB
testcase_29 AC 123 ms
56,428 KB
testcase_30 AC 121 ms
55,588 KB
testcase_31 AC 121 ms
55,804 KB
testcase_32 AC 126 ms
55,556 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(boo[N]){
                System.out.println(c[N]);
                return;
            }
        }
        System.out.println(-1);
    }
}
0