結果

問題 No.3 ビットすごろく
ユーザー k_kadora
提出日時 2015-06-13 04:56:20
言語 Java
(openjdk 23)
結果
AC  
実行時間 143 ms / 5,000 ms
コード長 988 bytes
コンパイル時間 3,529 ms
コンパイル使用メモリ 78,696 KB
実行使用メモリ 41,796 KB
最終ジャッジ日時 2024-07-01 07:25:20
合計ジャッジ時間 9,115 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;
import java.util.ArrayList;

public class Bit{
	public int count(int n){
		int c=0;
		while(n > 0){
			c += n % 2;
			n = n/2;
		}
		return c;
	}
	public int search(int n){
		ArrayList<Integer> list = new ArrayList<>();
		ArrayList<Integer> next = new ArrayList<>();
		boolean[] v = new boolean[n+1];
		int p = 1,ne,pre,move,res = 1;
		list.add(1);
		while(list.size()>0){
			for(int i = 0;i<list.size();i++){
				p = list.get(i);
				move = count(p);
				ne = p + move;
				pre = p - move;
				if(p == n)return res;
				if(ne <= n && v[ne] == false){
					v[ne] = true;
					next.add(ne);
				}
				if(pre >= 1 && v[pre] == false){
					v[pre] = true;
					next.add(pre);
				}
			}
			res++;
			list.clear();
			list.addAll(next);
			next.clear();
		}
		return -1;
	}

	public static void main(String[] arg){
		Scanner sc = new Scanner(System.in);
		Bit cluc = new Bit();
		int n,res;
		n = sc.nextInt();
		res = cluc.search(n);
		System.out.println(res);
	}
}
0