結果

問題 No.3 ビットすごろく
ユーザー shin
提出日時 2022-05-17 16:18:26
言語 Java
(openjdk 23)
結果
WA  
実行時間 -
コード長 1,184 bytes
コンパイル時間 3,738 ms
コンパイル使用メモリ 78,756 KB
実行使用メモリ 43,220 KB
最終ジャッジ日時 2024-09-15 11:01:25
合計ジャッジ時間 8,052 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 15 WA * 18
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Queue;

public class No3 {

	public static void main(String[] args) throws IOException{
		//ビットすごろく
		int N = Integer.parseInt(readStr()[0]) , k = 0;
		int[] root = new int[N + 1];
		root[1] = 1;
		Queue<Integer> qu = new ArrayDeque<>();
		qu.add(1);
		
		
		while(qu.size() > 0 && (k = qu.poll()) < N) {
			if(k + Integer.bitCount(k) <= N) {
				root[k + Integer.bitCount(k)] = root[k] + 1;
				qu.add(k + Integer.bitCount(k));
			}
			if(k - Integer.bitCount(k) >= 1 && root[k - Integer.bitCount(k)] == 0) {
				root[k - Integer.bitCount(k)] = root[k] + 1;
				qu.add(k - Integer.bitCount(k));
			}
		}
		
		System.out.println(root[N] == 0 ? -1 : root[N]);

	}
	
	public static String[] readStr() throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		ArrayList<String> list = new ArrayList<>();

		do {
			list.add(br.readLine());
		}while(br.ready());

		br.close();

		String[] text = new String[list.size()];
		list.toArray(text);

		return text;

	}

}
0