結果

問題 No.3 ビットすごろく
ユーザー fal_rnd
提出日時 2016-11-10 00:16:01
言語 Java
(openjdk 23)
結果
AC  
実行時間 142 ms / 5,000 ms
コード長 654 bytes
コンパイル時間 2,069 ms
コンパイル使用メモリ 78,252 KB
実行使用メモリ 54,688 KB
最終ジャッジ日時 2024-07-01 08:09:45
合計ジャッジ時間 7,444 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
class B{
	static Scanner s = new Scanner(System.in);
	public static void main(String[] args) {
		int in = Integer.parseInt(s.next());
		int[] m = new int[in];
		Queue<Integer> q = new ArrayDeque<Integer>();

		Arrays.fill(m, Integer.MAX_VALUE);
		m[0]=1;

		q.add(Integer.valueOf(0));
		while(!q.isEmpty()) {
			int i = q.poll(), d = Integer.bitCount(i+1),
				a = i-d, b = i+d;

			if(a>=0        && m[a]>m[i]+1) {
				m[a]=m[i]+1;
				q.add(a);
			}
			if(b< in && m[b]>m[i]+1) {
				m[b]=m[i]+1;
				q.add(b);
			}
		}
		if(m[in-1]==Integer.MAX_VALUE) {
			System.out.println(-1);
		}else {
			System.out.println(m[in-1]);
		}
	}
}
0