結果

問題 No.3 ビットすごろく
ユーザー villachvillach
提出日時 2017-09-19 00:43:21
言語 Java
(openjdk 23)
結果
WA  
実行時間 -
コード長 1,063 bytes
コンパイル時間 3,587 ms
コンパイル使用メモリ 77,968 KB
実行使用メモリ 42,336 KB
最終ジャッジ日時 2024-11-08 02:42:04
合計ジャッジ時間 11,433 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 32 WA * 1
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder;

import java.util.LinkedList;
import java.util.Scanner;

public class N3 {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		LinkedList<Integer> queue = new LinkedList<Integer>();
		LinkedList<Integer> visited = new LinkedList<Integer>();
		int i = 1;
		int a, b, dist;
		
		for(int j = 0;j <= n;++j){
			visited.add(0);
		}
		
		if(queue.size() == 1){
			System.out.println(0);
			return;
		}
		
		while(true){
			dist = visited.get(i);
			a = i - countBin(i);
			b = i + countBin(i);
			if(a > 0 && a <= n && visited.get(a) == 0){
				visited.set(a, dist + 1);
				queue.offer(a);
			}
			if(b > 0 && b <= n && visited.get(b) == 0){
				visited.set(b, dist + 1);
				queue.offer(b);
			}
			
			if(a == n || b == n){
				System.out.println(dist + 2);
				return;
			}
			
			if(queue.size() == 0){
				System.out.println(-1);
				return;
			}
			i = queue.poll();
		}
	}
	
	public static int countBin(int x){
		int i = 0;
		while(x != 0){
			i += x % 2;
			x /= 2;
		}
		return i;
	}
}
0