結果

問題 No.3 ビットすごろく
ユーザー kohaku_kohaku
提出日時 2016-12-12 00:39:48
言語 Java
(openjdk 23)
結果
AC  
実行時間 145 ms / 5,000 ms
コード長 1,017 bytes
コンパイル時間 2,233 ms
コンパイル使用メモリ 78,668 KB
実行使用メモリ 54,448 KB
最終ジャッジ日時 2024-07-01 08:12:04
合計ジャッジ時間 7,769 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

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