結果

問題 No.3 ビットすごろく
ユーザー rn4rurn4ru
提出日時 2016-04-12 23:06:43
言語 Java21
(openjdk 21)
結果
AC  
実行時間 2,429 ms / 5,000 ms
コード長 1,004 bytes
コンパイル時間 2,050 ms
コンパイル使用メモリ 74,952 KB
実行使用メモリ 78,116 KB
最終ジャッジ日時 2023-09-13 23:51:19
合計ジャッジ時間 25,775 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 125 ms
55,468 KB
testcase_01 AC 124 ms
55,792 KB
testcase_02 AC 126 ms
55,728 KB
testcase_03 AC 179 ms
58,716 KB
testcase_04 AC 153 ms
56,120 KB
testcase_05 AC 237 ms
60,056 KB
testcase_06 AC 177 ms
58,184 KB
testcase_07 AC 168 ms
57,820 KB
testcase_08 AC 199 ms
59,396 KB
testcase_09 AC 466 ms
66,408 KB
testcase_10 AC 933 ms
67,192 KB
testcase_11 AC 359 ms
61,732 KB
testcase_12 AC 242 ms
60,112 KB
testcase_13 AC 169 ms
57,812 KB
testcase_14 AC 818 ms
67,432 KB
testcase_15 AC 2,048 ms
74,556 KB
testcase_16 AC 1,106 ms
67,304 KB
testcase_17 AC 1,807 ms
73,624 KB
testcase_18 AC 156 ms
59,548 KB
testcase_19 AC 2,391 ms
77,596 KB
testcase_20 AC 139 ms
55,956 KB
testcase_21 AC 127 ms
55,980 KB
testcase_22 AC 840 ms
67,440 KB
testcase_23 AC 2,418 ms
78,116 KB
testcase_24 AC 2,429 ms
77,904 KB
testcase_25 AC 2,070 ms
74,424 KB
testcase_26 AC 126 ms
55,992 KB
testcase_27 AC 177 ms
58,152 KB
testcase_28 AC 1,048 ms
67,352 KB
testcase_29 AC 357 ms
62,068 KB
testcase_30 AC 132 ms
55,680 KB
testcase_31 AC 128 ms
55,952 KB
testcase_32 AC 295 ms
60,044 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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);

	}

}
0