結果

問題 No.3 ビットすごろく
ユーザー yuki2006yuki2006
提出日時 2014-09-30 02:10:32
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,440 bytes
コンパイル時間 2,135 ms
コンパイル使用メモリ 75,312 KB
実行使用メモリ 57,664 KB
最終ジャッジ日時 2023-08-28 19:54:35
合計ジャッジ時間 7,299 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 119 ms
55,488 KB
testcase_03 AC 119 ms
56,144 KB
testcase_04 WA -
testcase_05 AC 119 ms
55,508 KB
testcase_06 AC 120 ms
55,692 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 123 ms
55,704 KB
testcase_13 AC 118 ms
55,760 KB
testcase_14 AC 118 ms
55,444 KB
testcase_15 AC 119 ms
53,800 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 117 ms
55,676 KB
testcase_23 AC 120 ms
56,000 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 119 ms
55,468 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
権限があれば一括ダウンロードができます

ソースコード

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));
        while (queue.size() > 0) {

            Tuple v = queue.pop();
            if (v.a == n) {
                return v.b;
            }


            check[v.a] = true;

            int bitCount = getBitCount(v.a);

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


            if (a > 0) {
                if (!check[a]) {
                    queue.push(new Tuple(a, v.b + 1));
                }
            }
            if (b <= n) {
                if (!check[b]) {
                    queue.push(new Tuple(a, 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