結果

問題 No.3 ビットすごろく
ユーザー yuki2006yuki2006
提出日時 2014-09-30 03:17:08
言語 Java21
(openjdk 21)
結果
AC  
実行時間 177 ms / 5,000 ms
コード長 1,473 bytes
コンパイル時間 4,988 ms
コンパイル使用メモリ 74,540 KB
実行使用メモリ 59,024 KB
最終ジャッジ日時 2023-09-13 22:48:53
合計ジャッジ時間 9,263 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 122 ms
56,420 KB
testcase_01 AC 123 ms
56,084 KB
testcase_02 AC 121 ms
55,800 KB
testcase_03 AC 143 ms
55,932 KB
testcase_04 AC 130 ms
55,880 KB
testcase_05 AC 159 ms
58,164 KB
testcase_06 AC 147 ms
58,392 KB
testcase_07 AC 136 ms
57,568 KB
testcase_08 AC 154 ms
57,960 KB
testcase_09 AC 162 ms
58,528 KB
testcase_10 AC 166 ms
56,764 KB
testcase_11 AC 160 ms
56,868 KB
testcase_12 AC 160 ms
58,624 KB
testcase_13 AC 138 ms
57,792 KB
testcase_14 AC 166 ms
58,356 KB
testcase_15 AC 173 ms
58,756 KB
testcase_16 AC 168 ms
57,956 KB
testcase_17 AC 172 ms
58,456 KB
testcase_18 AC 135 ms
57,848 KB
testcase_19 AC 171 ms
58,208 KB
testcase_20 AC 125 ms
56,108 KB
testcase_21 AC 123 ms
55,556 KB
testcase_22 AC 166 ms
58,224 KB
testcase_23 AC 177 ms
58,492 KB
testcase_24 AC 175 ms
58,128 KB
testcase_25 AC 172 ms
58,284 KB
testcase_26 AC 121 ms
55,728 KB
testcase_27 AC 143 ms
56,784 KB
testcase_28 AC 171 ms
58,676 KB
testcase_29 AC 161 ms
58,176 KB
testcase_30 AC 123 ms
55,768 KB
testcase_31 AC 123 ms
54,436 KB
testcase_32 AC 160 ms
59,024 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]) {

                    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