結果

問題 No.3 ビットすごろく
ユーザー yuki2006yuki2006
提出日時 2015-05-31 17:56:52
言語 Java21
(openjdk 21)
結果
AC  
実行時間 137 ms / 5,000 ms
コード長 1,577 bytes
コンパイル時間 3,396 ms
コンパイル使用メモリ 76,480 KB
実行使用メモリ 57,436 KB
最終ジャッジ日時 2023-09-13 23:12:19
合計ジャッジ時間 9,111 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 122 ms
55,488 KB
testcase_01 AC 119 ms
56,100 KB
testcase_02 AC 128 ms
54,768 KB
testcase_03 AC 126 ms
55,836 KB
testcase_04 AC 123 ms
55,508 KB
testcase_05 AC 128 ms
55,444 KB
testcase_06 AC 130 ms
55,820 KB
testcase_07 AC 128 ms
55,892 KB
testcase_08 AC 128 ms
55,696 KB
testcase_09 AC 133 ms
57,436 KB
testcase_10 AC 130 ms
56,076 KB
testcase_11 AC 133 ms
55,988 KB
testcase_12 AC 136 ms
55,692 KB
testcase_13 AC 133 ms
55,796 KB
testcase_14 AC 134 ms
55,900 KB
testcase_15 AC 130 ms
55,744 KB
testcase_16 AC 128 ms
55,708 KB
testcase_17 AC 130 ms
56,052 KB
testcase_18 AC 121 ms
55,760 KB
testcase_19 AC 129 ms
55,764 KB
testcase_20 AC 123 ms
56,248 KB
testcase_21 AC 121 ms
55,992 KB
testcase_22 AC 130 ms
56,448 KB
testcase_23 AC 133 ms
55,896 KB
testcase_24 AC 129 ms
55,724 KB
testcase_25 AC 132 ms
56,088 KB
testcase_26 AC 129 ms
56,060 KB
testcase_27 AC 133 ms
55,480 KB
testcase_28 AC 137 ms
55,512 KB
testcase_29 AC 134 ms
55,680 KB
testcase_30 AC 123 ms
55,448 KB
testcase_31 AC 119 ms
55,944 KB
testcase_32 AC 125 ms
56,096 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Scanner;

public class Yuki003 {
    int bitCount(int value) {
        int count = 0;
        while (value > 0) {
            count += value % 2;

            value /= 2;
        }
        return count;
    }

    Yuki003() {
        Scanner scanner = new Scanner(System.in);

        int N = scanner.nextInt();

        System.out.println(search(N));


    }

    private int search(final int N) {
        boolean[] visit = new boolean[N + 1];
        ArrayList<Integer> list = new ArrayList<>();
        ArrayList<Integer> next = new ArrayList<>();
        list.add(1);
        int move = 1;
        visit[1] = true;
        while (list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                final Integer value = list.get(i);
                if (value == N) {
                    return move;
                }

                int bit = bitCount(value);
                int prev = value - bit;
                int nxt = value + bit;
                if (1 <= prev && !visit[prev]) {
                    next.add(prev);
                    visit[prev] = true;

                }
                if (nxt <= N && !visit[nxt]) {
                    next.add(nxt);
                    visit[nxt] = true;

                }

            }
            list.clear();
            list.addAll(next);
            next.clear();
            move++;
        }
        return -1;
    }

    public static void main(String[] args) {
        new Yuki003();

    }
}
0