結果

問題 No.3 ビットすごろく
ユーザー villachvillach
提出日時 2017-09-19 00:43:21
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,063 bytes
コンパイル時間 3,381 ms
コンパイル使用メモリ 78,664 KB
実行使用メモリ 54,448 KB
最終ジャッジ日時 2024-04-25 15:37:36
合計ジャッジ時間 11,058 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 116 ms
52,848 KB
testcase_01 AC 132 ms
54,008 KB
testcase_02 AC 116 ms
53,016 KB
testcase_03 AC 141 ms
53,220 KB
testcase_04 AC 156 ms
54,256 KB
testcase_05 AC 191 ms
54,440 KB
testcase_06 AC 155 ms
54,168 KB
testcase_07 AC 149 ms
54,156 KB
testcase_08 AC 174 ms
54,032 KB
testcase_09 AC 220 ms
54,380 KB
testcase_10 AC 254 ms
54,232 KB
testcase_11 AC 209 ms
54,096 KB
testcase_12 AC 177 ms
53,284 KB
testcase_13 AC 136 ms
53,140 KB
testcase_14 AC 248 ms
54,424 KB
testcase_15 AC 292 ms
54,148 KB
testcase_16 AC 273 ms
54,224 KB
testcase_17 AC 290 ms
54,276 KB
testcase_18 AC 153 ms
54,176 KB
testcase_19 AC 298 ms
54,420 KB
testcase_20 AC 152 ms
54,212 KB
testcase_21 AC 132 ms
54,180 KB
testcase_22 AC 248 ms
54,396 KB
testcase_23 AC 298 ms
54,344 KB
testcase_24 AC 297 ms
54,160 KB
testcase_25 AC 294 ms
54,072 KB
testcase_26 WA -
testcase_27 AC 150 ms
54,204 KB
testcase_28 AC 269 ms
53,980 KB
testcase_29 AC 211 ms
54,432 KB
testcase_30 AC 134 ms
54,004 KB
testcase_31 AC 134 ms
54,240 KB
testcase_32 AC 202 ms
54,172 KB
権限があれば一括ダウンロードができます

ソースコード

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