結果

問題 No.3 ビットすごろく
ユーザー villachvillach
提出日時 2017-09-19 00:45:43
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,052 bytes
コンパイル時間 3,258 ms
コンパイル使用メモリ 78,244 KB
実行使用メモリ 55,968 KB
最終ジャッジ日時 2024-04-25 15:38:38
合計ジャッジ時間 10,200 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 114 ms
53,860 KB
testcase_01 AC 99 ms
52,384 KB
testcase_02 AC 111 ms
54,284 KB
testcase_03 AC 128 ms
53,104 KB
testcase_04 AC 138 ms
54,156 KB
testcase_05 AC 154 ms
53,444 KB
testcase_06 AC 138 ms
54,248 KB
testcase_07 AC 130 ms
53,320 KB
testcase_08 AC 157 ms
54,112 KB
testcase_09 AC 210 ms
54,436 KB
testcase_10 AC 242 ms
54,444 KB
testcase_11 AC 191 ms
54,044 KB
testcase_12 AC 177 ms
54,128 KB
testcase_13 AC 142 ms
54,176 KB
testcase_14 AC 232 ms
54,376 KB
testcase_15 AC 271 ms
54,324 KB
testcase_16 AC 236 ms
53,260 KB
testcase_17 AC 264 ms
54,196 KB
testcase_18 AC 121 ms
53,124 KB
testcase_19 AC 271 ms
53,608 KB
testcase_20 AC 131 ms
55,968 KB
testcase_21 AC 110 ms
53,108 KB
testcase_22 AC 215 ms
54,000 KB
testcase_23 AC 254 ms
53,364 KB
testcase_24 AC 266 ms
53,992 KB
testcase_25 AC 255 ms
53,632 KB
testcase_26 WA -
testcase_27 AC 139 ms
54,076 KB
testcase_28 AC 232 ms
53,244 KB
testcase_29 AC 181 ms
53,236 KB
testcase_30 AC 120 ms
54,000 KB
testcase_31 AC 123 ms
54,160 KB
testcase_32 AC 196 ms
54,124 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(n == 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