結果
| 問題 | No.3 ビットすごろく | 
| コンテスト | |
| ユーザー |  yonaka_ggg | 
| 提出日時 | 2020-09-09 21:23:56 | 
| 言語 | Java (openjdk 23) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 149 ms / 5,000 ms | 
| コード長 | 1,056 bytes | 
| コンパイル時間 | 2,422 ms | 
| コンパイル使用メモリ | 75,628 KB | 
| 実行使用メモリ | 56,020 KB | 
| 最終ジャッジ日時 | 2024-07-01 09:56:02 | 
| 合計ジャッジ時間 | 7,907 ms | 
| ジャッジサーバーID (参考情報) | judge3 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 33 | 
ソースコード
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Scanner;
// 解説AC
class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int[] d = new int[N + 1];
        final int INF = Integer.MAX_VALUE / 2;
        Arrays.fill(d, INF);
        d[1] = 1;
        ArrayDeque<Integer> deque = new ArrayDeque<Integer>(N * 2); //適当な初期容量
        deque.add(1);
        while(deque.isEmpty() == false) {
            int nowPosition = deque.poll();
            int move = Integer.bitCount(nowPosition);
            for(int i = -move; i <= move; i += move *2) {
                int nextPosition = nowPosition + i;
                if(nextPosition < 1 || nextPosition > N) continue;
                if(d[nextPosition] != INF) continue;
                d[nextPosition] = d[nowPosition] + 1;
                deque.add(nextPosition);
            }
        }
        if(d[N] == INF) System.out.println(-1);
        else System.out.println(d[N]);
    }
}
            
            
            
        