結果

問題 No.3 ビットすごろく
コンテスト
ユーザー SilverBullet _Rye
提出日時 2023-12-02 13:55:14
言語 Java
(openjdk 26.0.2.1)
コンパイル:
javac -encoding UTF8 _filename_
実行:
java -ea -Xmx700m -Xss256M -DONLINE_JUDGE=true _class_
結果
AC  
実行時間 58 ms / 5,000 ms
+ 169µs
コード長 763 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,296 ms
コンパイル使用メモリ 85,724 KB
実行使用メモリ 43,588 KB
最終ジャッジ日時 2026-09-05 00:18:21
合計ジャッジ時間 4,897 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] cnt = new int[n];
        Queue<Integer> target = new ArrayDeque<>();
        target.add(1);
        cnt[0]=1;
        while (!target.isEmpty()){
            int now = target.poll();
            if(now==n){
                System.out.println(cnt[n-1]);
                return;
            }
            int bit = Integer.bitCount(now);
            if(now-bit>=1 && cnt[now-bit-1] ==0){target.add(now-bit);cnt[now-bit-1] = cnt[now-1]+1;}
            if(now+bit<=n && cnt[now+bit-1] ==0){target.add(now+bit);cnt[now+bit-1] = cnt[now-1]+1;}
        }
        System.out.println(-1);
    }
}
0