結果
| 問題 | No.3 ビットすごろく | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2016-04-12 23:06:43 | 
| 言語 | Java (openjdk 23) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 2,394 ms / 5,000 ms | 
| コード長 | 1,004 bytes | 
| コンパイル時間 | 2,059 ms | 
| コンパイル使用メモリ | 77,792 KB | 
| 実行使用メモリ | 76,428 KB | 
| 最終ジャッジ日時 | 2024-07-01 07:50:44 | 
| 合計ジャッジ時間 | 25,264 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 33 | 
ソースコード
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.Scanner;
class Pair {
	public Pair(int pos, int step) {
		this.pos = pos;
		this.step = step;
	}
	int pos;
	int step;
}
public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int N = scanner.nextInt();
		boolean[] visited = new boolean[N + 1];
		Queue<Pair> q = new ArrayDeque<>();
		q.add(new Pair(1, 1));
		while (!q.isEmpty()) {
			Pair p = q.poll();
			visited[p.pos] = true;
			if (p.pos == N) {
				System.out.println(p.step);
				return;
			}
			char[] binary = Integer.toBinaryString(p.pos).toCharArray();
			int move = 0;
			for (int i = 0; i < binary.length; i++) {
				if (binary[i] == '1') {
					move++;
				}
			}
			if (p.pos + move <= N && !visited[p.pos + move]) {
				q.add(new Pair(p.pos + move, p.step + 1));
			}
			if (p.pos - move > 0 && !visited[p.pos - move]) {
				q.add(new Pair(p.pos - move, p.step + 1));
			}
		}
		System.out.println(-1);
	}
}
            
            
            
        