結果
| 問題 | No.3 ビットすごろく | 
| コンテスト | |
| ユーザー |  101000010 | 
| 提出日時 | 2017-05-17 04:24:35 | 
| 言語 | Java (openjdk 23) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 311 ms / 5,000 ms | 
| コード長 | 1,964 bytes | 
| コンパイル時間 | 3,659 ms | 
| コンパイル使用メモリ | 77,824 KB | 
| 実行使用メモリ | 47,456 KB | 
| 最終ジャッジ日時 | 2024-07-01 08:43:00 | 
| 合計ジャッジ時間 | 12,371 ms | 
| ジャッジサーバーID (参考情報) | judge2 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 33 | 
ソースコード
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();
    }
}
            
            
            
        