結果

問題 No.3 ビットすごろく
ユーザー tsunabittsunabit
提出日時 2019-08-11 21:39:30
言語 Java21
(openjdk 21)
結果
AC  
実行時間 246 ms / 5,000 ms
コード長 883 bytes
コンパイル時間 3,506 ms
コンパイル使用メモリ 74,688 KB
実行使用メモリ 60,508 KB
最終ジャッジ日時 2023-09-14 01:25:08
合計ジャッジ時間 11,547 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 126 ms
53,460 KB
testcase_01 AC 124 ms
55,448 KB
testcase_02 AC 127 ms
55,448 KB
testcase_03 AC 209 ms
59,304 KB
testcase_04 AC 183 ms
57,852 KB
testcase_05 AC 228 ms
59,444 KB
testcase_06 AC 215 ms
60,276 KB
testcase_07 AC 191 ms
59,848 KB
testcase_08 AC 227 ms
59,260 KB
testcase_09 AC 235 ms
59,392 KB
testcase_10 AC 228 ms
60,312 KB
testcase_11 AC 234 ms
59,084 KB
testcase_12 AC 224 ms
59,724 KB
testcase_13 AC 202 ms
59,240 KB
testcase_14 AC 234 ms
59,436 KB
testcase_15 AC 242 ms
59,448 KB
testcase_16 AC 242 ms
60,508 KB
testcase_17 AC 234 ms
59,384 KB
testcase_18 AC 193 ms
59,212 KB
testcase_19 AC 243 ms
59,460 KB
testcase_20 AC 181 ms
57,552 KB
testcase_21 AC 128 ms
55,672 KB
testcase_22 AC 236 ms
60,072 KB
testcase_23 AC 246 ms
59,444 KB
testcase_24 AC 245 ms
59,672 KB
testcase_25 AC 244 ms
59,836 KB
testcase_26 AC 126 ms
55,508 KB
testcase_27 AC 205 ms
58,920 KB
testcase_28 AC 239 ms
59,236 KB
testcase_29 AC 231 ms
60,088 KB
testcase_30 AC 133 ms
55,812 KB
testcase_31 AC 138 ms
56,484 KB
testcase_32 AC 226 ms
59,616 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;
import java.math.*;

public class No3 {
    public static void main(String[] args) {
    	Scanner sc = new Scanner(System.in);
    	int n = sc.nextInt();
    	// BFS
    	int[] step = new int[n];
    	Arrays.fill(step, 0);
    	Queue<Integer> queue = new ArrayDeque<Integer>();
    	queue.add(1);
    	while(queue.peek() != null) {
    		int t = queue.poll();
    		if(t == n) {
    			System.out.println(step[t - 1] + 1);
    			return;
    		}
    		int ido = Integer.toBinaryString(t).replaceAll("0", "").length();
    		int tugi = t + ido;
    		if(tugi <= n && step[tugi - 1] == 0) {
    			step[tugi - 1] = step[t - 1] + 1;
    			queue.add(tugi);
    		}
    		tugi = t - ido;
    		if(1 <= tugi && step[tugi -1] == 0) {
    			step[tugi - 1] = step[t - 1] + 1;
    			queue.add(tugi);
    		}
    	}
    	System.out.println(-1);
    }
}
0