結果

問題 No.3 ビットすごろく
ユーザー kou6839kou6839
提出日時 2014-11-08 13:44:56
言語 Java
(openjdk 23)
結果
AC  
実行時間 145 ms / 5,000 ms
コード長 736 bytes
コンパイル時間 2,920 ms
コンパイル使用メモリ 75,384 KB
実行使用メモリ 54,660 KB
最終ジャッジ日時 2024-07-01 07:06:10
合計ジャッジ時間 8,491 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
 

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N=sc.nextInt();
		int ans=1;
		int[] check=new int[N+1];
		Queue<Integer> queue = new LinkedList<>();
		queue.offer(1);
		check[1]=1;
		while(!queue.isEmpty()){
			int temp1 = queue.poll();
			int temp2 = temp1+Integer.bitCount(temp1);
			int temp3=temp1-Integer.bitCount(temp1);
			if(temp2<=N&&check[temp2]==0){
				queue.offer(temp2);
				check[temp2]=check[temp1]+1;
			}
			if(temp3>=1&&check[temp3]==0){
				queue.offer(temp3);
				check[temp3]=check[temp1]+1;
			}
		}
		if(check[N]==0){
			System.out.println(-1);
		}else{
			System.out.println(check[N]);
		}
	}
}	
0