結果

問題 No.3 ビットすごろく
ユーザー トミートミー
提出日時 2024-04-01 01:29:36
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,484 bytes
コンパイル時間 2,988 ms
コンパイル使用メモリ 77,884 KB
実行使用メモリ 58,200 KB
最終ジャッジ日時 2024-04-01 01:29:51
合計ジャッジ時間 9,756 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 138 ms
57,608 KB
testcase_01 AC 154 ms
57,676 KB
testcase_02 AC 137 ms
57,576 KB
testcase_03 AC 147 ms
57,864 KB
testcase_04 WA -
testcase_05 AC 151 ms
57,572 KB
testcase_06 AC 154 ms
57,564 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 153 ms
57,676 KB
testcase_13 AC 140 ms
57,956 KB
testcase_14 AC 155 ms
57,680 KB
testcase_15 AC 154 ms
57,676 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 135 ms
57,976 KB
testcase_22 AC 148 ms
57,572 KB
testcase_23 AC 179 ms
57,576 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 136 ms
57,556 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Integer n = sc.nextInt();
        Integer depth = 0;
        Deque<Integer> dq = new ArrayDeque<>();
        boolean solved = false;
        boolean seen[] = new boolean[n + 1];
        seen[1] = true;
        for (int i = 2; i <= n; i++) {
            seen[i] = false;
        }
        dq.addFirst(1);
        while (!dq.isEmpty()) {
            Integer adj = dq.getFirst();
            boolean found = false;
            dq.removeFirst();
            depth++;
            if (adj == n) {
                System.out.println(depth);
                solved = true;
                break;
            }
            Integer cnt = countBit(adj);
            if (adj - cnt >= 1 && !seen[adj - cnt]) {
                dq.addLast(adj - cnt);
                seen[adj - cnt] = true;
                found = true;
            }
            if (adj + cnt <= n && !seen[adj + cnt]) {
                dq.addLast(adj + cnt);
                seen[adj + cnt] = true;
                found = true;
            }
            if (!found) {
                depth--;
            }
        }
        if (!solved) {
            System.out.println(-1);
        }
        sc.close();

    }

    static Integer countBit(Integer n) {
        Integer cnt = 0;
        while (n != 0) {
            cnt += n % 2;
            n /= 2;
        }
        return cnt;

    }
}
0