結果

問題 No.3 ビットすごろく
ユーザー yuki2006yuki2006
提出日時 2014-09-30 03:18:28
言語 Java21
(openjdk 21)
結果
AC  
実行時間 139 ms / 5,000 ms
コード長 1,509 bytes
コンパイル時間 2,161 ms
コンパイル使用メモリ 75,016 KB
実行使用メモリ 57,828 KB
最終ジャッジ日時 2023-09-13 22:49:59
合計ジャッジ時間 7,509 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 123 ms
55,908 KB
testcase_01 AC 124 ms
55,952 KB
testcase_02 AC 124 ms
55,792 KB
testcase_03 AC 132 ms
57,828 KB
testcase_04 AC 122 ms
55,816 KB
testcase_05 AC 123 ms
55,800 KB
testcase_06 AC 123 ms
55,756 KB
testcase_07 AC 125 ms
56,100 KB
testcase_08 AC 127 ms
55,660 KB
testcase_09 AC 128 ms
55,840 KB
testcase_10 AC 131 ms
55,432 KB
testcase_11 AC 127 ms
55,980 KB
testcase_12 AC 129 ms
55,648 KB
testcase_13 AC 126 ms
53,920 KB
testcase_14 AC 131 ms
56,236 KB
testcase_15 AC 139 ms
55,792 KB
testcase_16 AC 134 ms
55,768 KB
testcase_17 AC 131 ms
56,176 KB
testcase_18 AC 123 ms
55,488 KB
testcase_19 AC 129 ms
55,960 KB
testcase_20 AC 122 ms
55,860 KB
testcase_21 AC 127 ms
55,396 KB
testcase_22 AC 129 ms
55,808 KB
testcase_23 AC 130 ms
56,080 KB
testcase_24 AC 130 ms
55,664 KB
testcase_25 AC 130 ms
55,656 KB
testcase_26 AC 122 ms
55,720 KB
testcase_27 AC 123 ms
57,616 KB
testcase_28 AC 133 ms
55,864 KB
testcase_29 AC 125 ms
56,404 KB
testcase_30 AC 121 ms
55,784 KB
testcase_31 AC 123 ms
55,916 KB
testcase_32 AC 125 ms
55,620 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        boolean[] check = new boolean[N + 1];

        System.out.println(bfs(N, check));

    }

    public static class Tuple {
        public int a;
        public int b;

        Tuple(int a, int b) {
            this.a = a;
            this.b = b;
        }

    }

    private static int bfs(int n, boolean[] check) {
        LinkedList<Tuple> queue = new LinkedList<>();
        queue.add(new Tuple(1, 1));
        check[1] = true;

        while (queue.size() > 0) {
            Tuple v = queue.pollFirst();
            if (v.a == n) {
                return v.b;
            }

            int bitCount = getBitCount(v.a);

            int a = v.a - bitCount;
            int b = v.a + bitCount;

            if (a > 0) {
                if (!check[a]) {
                    check[a] = true;
                    queue.add(new Tuple(a, v.b + 1));
                }
            }
            if (b <= n) {
                if (!check[b]) {
                    check[b] = true;
                    queue.add(new Tuple(b, v.b + 1));
                }
            }
        }
        return -1;
    }


    static int getBitCount(int n) {
        int count;
        for (count = 0; n > 0; count++) {
            n = n & (n - 1);
        }
        return count;
    }

}
0