結果

問題 No.3 ビットすごろく
ユーザー chiho_miyakochiho_miyako
提出日時 2015-04-20 12:44:29
言語 Java
(openjdk 23)
結果
AC  
実行時間 742 ms / 5,000 ms
コード長 1,432 bytes
コンパイル時間 2,233 ms
コンパイル使用メモリ 77,564 KB
実行使用メモリ 248,912 KB
最終ジャッジ日時 2024-07-01 07:20:51
合計ジャッジ時間 14,761 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner koko = new Scanner(System.in);
        int n = koko.nextInt();
        boolean[][] goal = new boolean[2*n+1][n+1];
        goal[1][1]=true;
        boolean[] judge = new boolean[n+1];
        judge[1] = true;
        for(int i=1; i<2*n+1; i++){
            for(int j=1; j<n+1; j++){
                if(goal[i][j]){
                    String jud = Integer.toBinaryString(j);
                    char[] num = jud.toCharArray();
                    int count=0;
                    for(int k=0; k<num.length; k++){
                        if(num[k]=='1'){
                            count++;
                        }
                    }
                    if(j+count<n+1&&!judge[j+count]){
                        goal[i+1][j+count]=true;
                        judge[j+count]=true;
                    }
                    if(j-count>1&&!judge[j-count]){
                        goal[i+1][j-count]=true;
                        judge[j-count]=true;
                    }
                }
            }
        }
        boolean flag = false;
        for(int i=1; i<2*n+1; i++){
            if(goal[i][n]){
                System.out.println(i);
                flag=true;
                break;
            }
        }
        if(!flag){
            System.out.println(-1);
        }
        
    }
    
}
0