結果

問題 No.3 ビットすごろく
ユーザー yuki2006yuki2006
提出日時 2015-05-31 17:50:53
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 1,523 bytes
コンパイル時間 3,667 ms
コンパイル使用メモリ 77,120 KB
実行使用メモリ 88,872 KB
最終ジャッジ日時 2023-09-20 18:07:13
合計ジャッジ時間 17,744 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 122 ms
44,712 KB
testcase_01 AC 123 ms
39,580 KB
testcase_02 AC 126 ms
39,212 KB
testcase_03 AC 190 ms
44,420 KB
testcase_04 AC 143 ms
41,080 KB
testcase_05 AC 502 ms
54,128 KB
testcase_06 AC 191 ms
44,704 KB
testcase_07 AC 177 ms
44,180 KB
testcase_08 AC 254 ms
46,660 KB
testcase_09 AC 3,244 ms
88,872 KB
testcase_10 TLE -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
権限があれば一括ダウンロードができます

ソースコード

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<>();
        list.add(1);
        int move = 1;
        visit[1] = true;
        while (list.size() > 0) {
            ArrayList<Integer> next = new ArrayList<>();

            for (int i = 0; i < list.size(); i++) {
                final int 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[value] = true;

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

                }

            }
            list = next;
            move++;
        }
        return -1;
    }

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

    }
}
0