結果

問題 No.3 ビットすごろく
ユーザー shinwisteriashinwisteria
提出日時 2017-04-02 11:37:16
言語 Java21
(openjdk 21)
結果
AC  
実行時間 134 ms / 5,000 ms
コード長 763 bytes
コンパイル時間 3,201 ms
コンパイル使用メモリ 75,176 KB
実行使用メモリ 56,300 KB
最終ジャッジ日時 2023-09-14 00:38:16
合計ジャッジ時間 8,672 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 117 ms
56,112 KB
testcase_01 AC 116 ms
55,916 KB
testcase_02 AC 115 ms
56,032 KB
testcase_03 AC 118 ms
56,184 KB
testcase_04 AC 121 ms
55,692 KB
testcase_05 AC 124 ms
55,996 KB
testcase_06 AC 122 ms
56,084 KB
testcase_07 AC 122 ms
56,224 KB
testcase_08 AC 128 ms
56,020 KB
testcase_09 AC 125 ms
55,712 KB
testcase_10 AC 128 ms
55,964 KB
testcase_11 AC 125 ms
56,056 KB
testcase_12 AC 125 ms
55,860 KB
testcase_13 AC 123 ms
55,836 KB
testcase_14 AC 132 ms
55,652 KB
testcase_15 AC 132 ms
55,648 KB
testcase_16 AC 128 ms
56,076 KB
testcase_17 AC 131 ms
55,724 KB
testcase_18 AC 126 ms
55,980 KB
testcase_19 AC 131 ms
56,172 KB
testcase_20 AC 121 ms
55,804 KB
testcase_21 AC 122 ms
55,840 KB
testcase_22 AC 128 ms
56,300 KB
testcase_23 AC 127 ms
55,764 KB
testcase_24 AC 129 ms
56,000 KB
testcase_25 AC 127 ms
55,804 KB
testcase_26 AC 118 ms
55,800 KB
testcase_27 AC 121 ms
54,152 KB
testcase_28 AC 134 ms
54,056 KB
testcase_29 AC 123 ms
56,000 KB
testcase_30 AC 118 ms
55,664 KB
testcase_31 AC 120 ms
55,436 KB
testcase_32 AC 123 ms
55,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayDeque;
import java.util.Scanner;

public class Bitsugoroku {

	public static void main(String[] args) {
		Scanner s =new Scanner(System.in);
		int N = s.nextInt();
		s.close();
		int[] way = new int[N+1];
		way[1] = 1;
		ArrayDeque<Integer> queue = new ArrayDeque<>();
		queue.offer(1);
		while(queue.size() != 0){
			int p = queue.poll();
			if(p == N){
				break;
			}
			int b = Integer.bitCount(p);
			if(p+b <= N){
				if(way[p+b] == 0||way[p+b] > way[p]+1){
					queue.offer(p+b);
					way[p+b] = way[p] + 1;
				}
			}
			if(p-b > 0&&( way[p-b] == 0 || way[p-b] > way[p] + 1)){
				queue.offer(p-b);
				way[p-b] = way[p] + 1;
			}
		}
		if(way[N] == 0){
			System.out.println("-1");
		}else{
			System.out.println(way[N]);
		}
	}

}
0