結果

問題 No.3 ビットすごろく
コンテスト
ユーザー 101000010
提出日時 2017-05-17 04:24:35
言語 Java
(openjdk 25.0.2)
コンパイル:
javac -encoding UTF8 _filename_
実行:
java -ea -Xmx700m -Xss256M -DONLINE_JUDGE=true _class_
結果
AC  
実行時間 160 ms / 5,000 ms
コード長 1,964 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,649 ms
コンパイル使用メモリ 83,320 KB
実行使用メモリ 51,900 KB
最終ジャッジ日時 2026-03-22 05:22:54
合計ジャッジ時間 7,399 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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