結果

問題 No.3 ビットすごろく
ユーザー 101000010101000010
提出日時 2017-05-17 04:24:35
言語 Java21
(openjdk 21)
結果
AC  
実行時間 246 ms / 5,000 ms
コード長 1,964 bytes
コンパイル時間 3,496 ms
コンパイル使用メモリ 74,964 KB
実行使用メモリ 61,372 KB
最終ジャッジ日時 2023-09-14 00:43:11
合計ジャッジ時間 11,479 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 127 ms
55,948 KB
testcase_01 AC 130 ms
56,176 KB
testcase_02 AC 126 ms
54,312 KB
testcase_03 AC 207 ms
58,960 KB
testcase_04 AC 171 ms
57,920 KB
testcase_05 AC 222 ms
59,444 KB
testcase_06 AC 210 ms
59,168 KB
testcase_07 AC 202 ms
59,332 KB
testcase_08 AC 220 ms
61,372 KB
testcase_09 AC 235 ms
59,664 KB
testcase_10 AC 246 ms
60,644 KB
testcase_11 AC 228 ms
59,168 KB
testcase_12 AC 231 ms
59,324 KB
testcase_13 AC 207 ms
59,216 KB
testcase_14 AC 230 ms
59,600 KB
testcase_15 AC 245 ms
59,484 KB
testcase_16 AC 239 ms
59,352 KB
testcase_17 AC 241 ms
59,936 KB
testcase_18 AC 202 ms
59,708 KB
testcase_19 AC 232 ms
59,480 KB
testcase_20 AC 180 ms
57,308 KB
testcase_21 AC 125 ms
56,216 KB
testcase_22 AC 238 ms
58,072 KB
testcase_23 AC 241 ms
59,240 KB
testcase_24 AC 243 ms
59,696 KB
testcase_25 AC 242 ms
60,364 KB
testcase_26 AC 126 ms
55,816 KB
testcase_27 AC 204 ms
57,436 KB
testcase_28 AC 241 ms
59,484 KB
testcase_29 AC 227 ms
60,764 KB
testcase_30 AC 132 ms
55,968 KB
testcase_31 AC 137 ms
56,172 KB
testcase_32 AC 225 ms
59,612 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class No003 {
    
    static Queue<P> queue = new ArrayDeque<P>();
    static boolean[] checked;
    static int N;
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        N = sc.nextInt();
        checked = new boolean[N];
        System.out.println(search());
        
    }
    
    //幅優先探索
    static int search(){
        //キューに最初のマスを追加
        P start = new P(1);
        queue.add(start);
        start.moveDistance = 1;
        while(!queue.isEmpty()){
            //キューの先頭を取り出す
            P point = queue.poll();
            //ゴールなら終了
            if(point.place == N) return point.moveDistance;
            //移動先が未チェックならキューに追加
            if(point.place + point.count1 <= N && !checked[point.place + point.count1 -1]){
                P newPoint = new P(point.place + point.count1);
                newPoint.moveDistance = point.moveDistance+1;
                queue.add(newPoint);
            }
            if(point.place - point.count1 >= 1 && !checked[point.place - point.count1 -1]){
                P newPoint = new P(point.place - point.count1);
                newPoint.moveDistance = point.moveDistance+1;
                queue.add(newPoint);
            }
        }
        //ゴールできないので-1を返す
        return -1;
    }
}

class P{
    int moveDistance;
    int place;
    int count1;
    
    P(int n){
       place = n;
       String bin = Integer.toBinaryString(n); 
       count1 = countStringInString(bin,"1");
       //移動済みチェックをつける
       No003.checked[n-1] = true;
    }
    
    static int countStringInString(String target, String searchWord) {
        return (target.length() - target.replaceAll(searchWord, "").length()) / searchWord.length();
    }
}
0