結果

問題 No.3 ビットすごろく
ユーザー kohaku_kohakukohaku_kohaku
提出日時 2016-12-12 00:14:51
言語 Java19
(openjdk 21)
結果
WA  
実行時間 -
コード長 931 bytes
コンパイル時間 3,226 ms
コンパイル使用メモリ 74,472 KB
実行使用メモリ 56,256 KB
最終ジャッジ日時 2023-08-19 11:39:31
合計ジャッジ時間 7,891 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 126 ms
55,292 KB
testcase_01 AC 125 ms
55,508 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 126 ms
56,032 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 128 ms
55,692 KB
testcase_08 AC 127 ms
56,124 KB
testcase_09 AC 130 ms
55,560 KB
testcase_10 AC 135 ms
55,920 KB
testcase_11 AC 129 ms
55,988 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 135 ms
55,552 KB
testcase_17 AC 133 ms
56,184 KB
testcase_18 AC 128 ms
56,072 KB
testcase_19 AC 134 ms
55,688 KB
testcase_20 AC 128 ms
55,728 KB
testcase_21 AC 125 ms
55,784 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 134 ms
53,528 KB
testcase_25 AC 134 ms
55,796 KB
testcase_26 AC 126 ms
55,572 KB
testcase_27 AC 129 ms
55,824 KB
testcase_28 AC 134 ms
55,508 KB
testcase_29 AC 130 ms
55,804 KB
testcase_30 AC 124 ms
55,820 KB
testcase_31 AC 123 ms
55,872 KB
testcase_32 AC 127 ms
55,824 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]==0)c[N]=-1;
        System.out.println(c[N]);
    }
}
0