結果

問題 No.3 ビットすごろく
ユーザー jp_ste
提出日時 2016-03-04 13:30:22
言語 Java
(openjdk 23)
結果
AC  
実行時間 142 ms / 5,000 ms
コード長 990 bytes
コンパイル時間 2,056 ms
コンパイル使用メモリ 74,860 KB
実行使用メモリ 56,288 KB
最終ジャッジ日時 2024-07-01 07:46:29
合計ジャッジ時間 7,644 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

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