結果

問題 No.3 ビットすごろく
ユーザー SagTokiSagToki
提出日時 2018-05-21 14:33:31
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 3,303 bytes
コンパイル時間 4,214 ms
コンパイル使用メモリ 80,368 KB
実行使用メモリ 57,772 KB
最終ジャッジ日時 2023-09-11 00:20:33
合計ジャッジ時間 10,120 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 130 ms
55,700 KB
testcase_01 AC 130 ms
55,696 KB
testcase_02 AC 129 ms
55,852 KB
testcase_03 AC 132 ms
54,108 KB
testcase_04 AC 130 ms
55,720 KB
testcase_05 AC 131 ms
55,856 KB
testcase_06 AC 132 ms
55,816 KB
testcase_07 AC 134 ms
55,540 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 134 ms
55,488 KB
testcase_13 AC 134 ms
55,420 KB
testcase_14 AC 133 ms
55,744 KB
testcase_15 AC 133 ms
56,152 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 133 ms
56,140 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 131 ms
56,072 KB
testcase_22 AC 135 ms
55,848 KB
testcase_23 AC 134 ms
56,076 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 131 ms
55,600 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 131 ms
56,140 KB
testcase_32 WA -
権限があれば一括ダウンロードができます

ソースコード

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の値を上回ってしまったので左に戻るための条件
                }else 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