結果

問題 No.3 ビットすごろく
ユーザー mazeppa1108
提出日時 2015-06-05 01:28:04
言語 Java
(openjdk 23)
結果
WA  
実行時間 -
コード長 987 bytes
コンパイル時間 1,781 ms
コンパイル使用メモリ 77,556 KB
実行使用メモリ 54,096 KB
最終ジャッジ日時 2024-07-06 14:08:58
合計ジャッジ時間 6,724 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 20 WA * 12 RE * 1
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		boolean[] visited = new boolean[n];
		visited[0] = true;
		int searchList[] = new int[n];
		int countTable[] = new int[n];
		countTable[0] = 1;
		countTable[n-1] = n+1;
		searchList[0] = 1;
		int j = 1;
		int k = 0;
		while (searchList[k] != 0) {
			int move = 0;
			int i = searchList[k];
			int count = countTable[k];
			while (i < n) {
				visited[i] = true;
				move = Integer.bitCount(i);
				if (visited[i - move]) {
					i += move;
					count++;
					if (i == n) {
						countTable[n-1] = Math.min (count, countTable[n-1]);
					}
				} else {
					if (i - move >= 0) {
						searchList[j] = (i - move);
						visited[i - move] = true;
						countTable[j] = count + 1;
						j++;
					}
				}
			}
			k++;
		}
		if (countTable[n-1] == (n+1)) {
			System.out.println("-1");
		} else {
			System.out.println(countTable[n-1]);
		}
	}
}
0