結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 128 ms
54,128 KB
testcase_01 AC 129 ms
54,124 KB
testcase_02 AC 116 ms
53,144 KB
testcase_03 AC 157 ms
54,252 KB
testcase_04 AC 152 ms
54,408 KB
testcase_05 AC 192 ms
53,980 KB
testcase_06 AC 155 ms
54,108 KB
testcase_07 AC 151 ms
54,312 KB
testcase_08 AC 178 ms
54,436 KB
testcase_09 AC 225 ms
54,140 KB
testcase_10 AC 257 ms
54,504 KB
testcase_11 AC 212 ms
54,320 KB
testcase_12 AC 192 ms
54,360 KB
testcase_13 AC 154 ms
54,372 KB
testcase_14 AC 247 ms
54,440 KB
testcase_15 AC 297 ms
54,316 KB
testcase_16 AC 278 ms
54,276 KB
testcase_17 AC 291 ms
54,132 KB
testcase_18 AC 154 ms
54,224 KB
testcase_19 AC 301 ms
54,012 KB
testcase_20 AC 154 ms
54,112 KB
testcase_21 AC 131 ms
53,984 KB
testcase_22 AC 249 ms
54,348 KB
testcase_23 AC 291 ms
54,228 KB
testcase_24 AC 295 ms
54,244 KB
testcase_25 AC 292 ms
56,216 KB
testcase_26 WA -
testcase_27 AC 150 ms
54,304 KB
testcase_28 AC 269 ms
54,440 KB
testcase_29 AC 214 ms
54,504 KB
testcase_30 AC 138 ms
54,108 KB
testcase_31 AC 135 ms
54,432 KB
testcase_32 AC 207 ms
54,404 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);
		}
		
		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