結果

問題 No.3 ビットすごろく
コンテスト
ユーザー SilverBullet _Rye
提出日時 2023-12-02 13:50:32
言語 Java
(openjdk 25.0.2)
コンパイル:
javac -encoding UTF8 _filename_
実行:
java -ea -Xmx700m -Xss256M -DONLINE_JUDGE=true _class_
結果
MLE  
(最新)
TLE  
(最初)
実行時間 -
コード長 872 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,237 ms
コンパイル使用メモリ 83,964 KB
実行使用メモリ 766,652 KB
最終ジャッジ日時 2026-04-13 18:28:14
合計ジャッジ時間 25,821 ms
ジャッジサーバーID
(参考情報)
judge3_1 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 12 MLE * 4 -- * 17
権限があれば一括ダウンロードができます

ソースコード

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();
        Set<Integer> fin = new HashSet<>();
        Queue<Integer> target = new ArrayDeque<>();
        Queue<Integer> cntQ = new ArrayDeque<>();
        target.add(1);
        cntQ.add(1);
        while (!target.isEmpty()){
            int now = target.poll();
            if(now==n){
                System.out.println(cntQ.poll());
                return;
            }
            fin.add(now);
            int bit = Integer.bitCount(now);
            int cnt = cntQ.poll();
            if(now-bit>=1 && !fin.contains(now-bit)){target.add(now-bit);cntQ.add(cnt+1);}
            if(now+bit<=n && !fin.contains(now+bit)){target.add(now+bit);cntQ.add(cnt+1);}
        }
        System.out.println(-1);
    }
}
0