結果

問題 No.3 ビットすごろく
ユーザー n_gojon_gojo
提出日時 2020-04-10 18:08:58
言語 Java
(openjdk 23)
結果
WA  
実行時間 -
コード長 1,412 bytes
コンパイル時間 3,722 ms
コンパイル使用メモリ 78,892 KB
実行使用メモリ 50,924 KB
最終ジャッジ日時 2024-09-15 07:16:11
合計ジャッジ時間 9,829 ms
ジャッジサーバーID
(参考情報)
judge1 / judge6
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 24 WA * 9
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;

public class No3 {
    public static void main(String[] args) {
        // 標準入力から読み込む際に、Scannerオブジェクトを使う。
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        int ans = 1;
        Set oldNo = new HashSet<>();
        Set nowNo = new HashSet<>();
        Set nextNo = new HashSet<>();
        nowNo.add(1);
        oldNo.add(1);
        while (ans != 10000) {
            if (nowNo.contains(n)) {
                System.out.println(ans);
                return;
            }
            for(Iterator<Integer> iterator = nowNo.iterator(); iterator.hasNext(); ) {
                Integer value = iterator.next();
                int i = value;
                int s = 0;
                while (i > 0) {
                    s += i%2;
                    i/=2;
                }
                if (!oldNo.contains(value + s)) {
                    nextNo.add(value + s);
                    oldNo.add(value + s);
                }
                if (!oldNo.contains(value - s)) {
                    nextNo.add(value - s);
                    oldNo.add(value - s);
                }
            }
            nowNo = nextNo;
            nextNo = new HashSet<>();
            ans++;
        }
        System.out.println("-1");

    }
}
0