結果

問題 No.3 ビットすごろく
ユーザー SagTokiSagToki
提出日時 2018-05-21 15:01:31
言語 Java21
(openjdk 21)
結果
AC  
実行時間 134 ms / 5,000 ms
コード長 3,315 bytes
コンパイル時間 3,531 ms
コンパイル使用メモリ 76,024 KB
実行使用メモリ 57,924 KB
最終ジャッジ日時 2023-09-14 01:01:50
合計ジャッジ時間 9,117 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 121 ms
55,608 KB
testcase_01 AC 121 ms
55,672 KB
testcase_02 AC 122 ms
56,016 KB
testcase_03 AC 126 ms
56,000 KB
testcase_04 AC 125 ms
56,000 KB
testcase_05 AC 124 ms
55,772 KB
testcase_06 AC 125 ms
55,780 KB
testcase_07 AC 125 ms
56,180 KB
testcase_08 AC 128 ms
57,924 KB
testcase_09 AC 129 ms
55,808 KB
testcase_10 AC 133 ms
55,800 KB
testcase_11 AC 134 ms
56,016 KB
testcase_12 AC 126 ms
55,508 KB
testcase_13 AC 127 ms
56,032 KB
testcase_14 AC 134 ms
55,756 KB
testcase_15 AC 134 ms
55,840 KB
testcase_16 AC 134 ms
55,608 KB
testcase_17 AC 133 ms
55,976 KB
testcase_18 AC 125 ms
55,696 KB
testcase_19 AC 133 ms
55,860 KB
testcase_20 AC 125 ms
56,108 KB
testcase_21 AC 121 ms
53,956 KB
testcase_22 AC 134 ms
55,856 KB
testcase_23 AC 134 ms
56,096 KB
testcase_24 AC 130 ms
55,832 KB
testcase_25 AC 132 ms
55,728 KB
testcase_26 AC 122 ms
55,984 KB
testcase_27 AC 125 ms
55,848 KB
testcase_28 AC 131 ms
55,576 KB
testcase_29 AC 125 ms
55,840 KB
testcase_30 AC 119 ms
55,952 KB
testcase_31 AC 121 ms
55,884 KB
testcase_32 AC 125 ms
55,740 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;
import java.util.InputMismatchException;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Queue;

public class BitSugoroku {
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        try{
            //文字を入力して範囲が正しいかのチェックを行う
            int N = scanner.nextInt();
            if(N < 0 || N > 10000){
                System.out.println("数値は1以上10000以下で入力してください");
                System.exit(0);
            }
            //入力値分の長さのint型配列を用意する
            int[] Decimal = new int[N];
            //入力値分の長さのboolean型配列を用意する
            boolean[] Answer = new boolean[N];
            //boolean配列の要素すべてに初期値としてfalseを代入する
            for(int i = 0 ; i < N ; i++){
                Answer[i] = false;
            } 
            //int型配列を初期化する
            Arrays.fill(Decimal , Integer.MAX_VALUE);
            //キューを用意する
            Queue<Integer> queue = new ArrayDeque<>();
            //キューに最小の要素を入れる
            queue.offer(1);
            //Answerの1番目の値を決定する
            Answer[0]=true;
            //Decimalの1番目の値を決定する
            Decimal[0]=1;
            //キューが空になるまで処理を行う
            while(!queue.isEmpty()){
                //10進数で今いる場所の数字をnowとする
                int Now = queue.poll();
                //今いる場所を2進数にしたときの1の個数をBinaryとする
                int Binary = Integer.bitCount(Now); 
                //マスを右に進めていくための条件  
                if(Now + Binary <= N){
                    Decimal[Now + Binary -1] 
                        =Math.min(Decimal[Now + Binary - 1] , Decimal[Now - 1] + 1);
                    if(!Answer[Now + Binary - 1]) {
                        queue.add(Now + Binary);
                    }
                    //1度通過した場所はtrueに上書き
                    Answer[Now + Binary - 1] = true;
                //Nの値を上回ってしまったので左に戻るための条件
                }
                if(Now - Binary > 0) {
                    Decimal[Now - Binary - 1] 
                        =Math.min(Decimal[Now - Binary - 1] , Decimal[Now - 1] + 1);
                    if(!Answer[Now - Binary - 1]) {
                        queue.add(Now - Binary);
                    }
                    //1度通過した場所はtrueに上書き
                    Answer[Now - Binary - 1]=true;
                }
            }
                //到達できなかった場合(最後に上書きがされなかった場合)
            	if(Decimal[N - 1]==Integer.MAX_VALUE) {
                    System.out.println(-1);
                //到達した場合
                }else{
                    System.out.println(Decimal[N - 1]);
		}
        }catch(InputMismatchException e){
            System.out.println("数字を入力してください");
        }catch(Exception E){
            System.out.println("想定外のエラーです");
        }
    }
}
0