結果

問題 No.3 ビットすごろく
ユーザー jp_stejp_ste
提出日時 2016-03-04 13:30:22
言語 Java21
(openjdk 21)
結果
AC  
実行時間 135 ms / 5,000 ms
コード長 990 bytes
コンパイル時間 2,019 ms
コンパイル使用メモリ 71,552 KB
実行使用メモリ 57,768 KB
最終ジャッジ日時 2023-09-13 23:45:04
合計ジャッジ時間 7,749 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 125 ms
55,908 KB
testcase_01 AC 126 ms
55,720 KB
testcase_02 AC 125 ms
56,480 KB
testcase_03 AC 130 ms
55,788 KB
testcase_04 AC 126 ms
56,016 KB
testcase_05 AC 131 ms
56,032 KB
testcase_06 AC 130 ms
55,984 KB
testcase_07 AC 130 ms
55,976 KB
testcase_08 AC 133 ms
55,824 KB
testcase_09 AC 131 ms
56,036 KB
testcase_10 AC 131 ms
56,308 KB
testcase_11 AC 131 ms
56,380 KB
testcase_12 AC 131 ms
55,816 KB
testcase_13 AC 133 ms
55,896 KB
testcase_14 AC 131 ms
55,824 KB
testcase_15 AC 132 ms
55,852 KB
testcase_16 AC 135 ms
56,116 KB
testcase_17 AC 132 ms
55,680 KB
testcase_18 AC 130 ms
56,000 KB
testcase_19 AC 130 ms
55,872 KB
testcase_20 AC 127 ms
55,928 KB
testcase_21 AC 125 ms
55,844 KB
testcase_22 AC 131 ms
55,760 KB
testcase_23 AC 131 ms
57,768 KB
testcase_24 AC 131 ms
55,800 KB
testcase_25 AC 132 ms
55,904 KB
testcase_26 AC 123 ms
55,876 KB
testcase_27 AC 129 ms
56,156 KB
testcase_28 AC 130 ms
56,000 KB
testcase_29 AC 130 ms
55,844 KB
testcase_30 AC 128 ms
55,756 KB
testcase_31 AC 130 ms
55,524 KB
testcase_32 AC 130 ms
55,964 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    
    static int N;
    static int minCount[];
    
    public static void main(String[] args) {
        try (Scanner scan = new Scanner(System.in)) {
            N = scan.nextInt();
            minCount = new int[N+1];
            Arrays.fill(minCount, Integer.MAX_VALUE);
            minCount[1] = 1;
            
            solve(1, 1);
            System.out.println(minCount[N]==Integer.MAX_VALUE? -1:minCount[N]);
        }
    }
    
    static void solve(int p, int c) {        
        int front = p + Integer.bitCount(p);
        if(front <= N) {
            if(c+1 < minCount[front]) {
                minCount[front] = c+1;
                solve(front, c+1);
            }
        }
        
        int back = p - Integer.bitCount(p);
        if(back >= 1) {
            if(c+1 < minCount[back]) {
                minCount[back] = c+1;
                solve(back, c+1);
            }
        }
    }
}
0