結果

問題 No.3 ビットすごろく
ユーザー rn4rurn4ru
提出日時 2016-04-12 22:56:58
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 966 bytes
コンパイル時間 2,220 ms
コンパイル使用メモリ 78,364 KB
実行使用メモリ 70,092 KB
最終ジャッジ日時 2024-04-14 23:59:32
合計ジャッジ時間 25,074 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 132 ms
53,936 KB
testcase_01 AC 131 ms
54,312 KB
testcase_02 AC 130 ms
54,284 KB
testcase_03 AC 175 ms
56,924 KB
testcase_04 WA -
testcase_05 AC 258 ms
57,748 KB
testcase_06 AC 174 ms
57,072 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 243 ms
57,696 KB
testcase_13 AC 173 ms
56,484 KB
testcase_14 AC 782 ms
65,196 KB
testcase_15 AC 1,933 ms
67,944 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 133 ms
54,168 KB
testcase_22 AC 821 ms
65,080 KB
testcase_23 AC 2,332 ms
70,072 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 129 ms
54,164 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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<Integer> q = new ArrayDeque<>();
		q.add(1);
		int step = 1;
		while (!q.isEmpty()) {
			int pos = q.poll();
			visited[pos] = true;
			if (pos == N) {
				break;
			}
			char[] binary = Integer.toBinaryString(pos).toCharArray();
			int move = 0;
			for (int i = 0; i < binary.length; i++) {
				if (binary[i] == '1') {
					move++;
				}
			}
			boolean moved = false;
			if (pos + move <= N && !visited[pos + move]) {
				q.add(pos + move);
				moved = true;
			}
			if (pos - move > 0 && !visited[pos - move]) {
				q.add(pos - move);
				moved = true;
			}
			if (moved)
				step++;
		}

		if (visited[N]) {
			System.out.println(step);
		} else {
			System.out.println(-1);
		}
		step++;
	}

}
0