結果

問題 No.3 ビットすごろく
ユーザー dogwood0424dogwood0424
提出日時 2017-12-05 10:15:37
言語 Java21
(openjdk 21)
結果
AC  
実行時間 145 ms / 5,000 ms
コード長 967 bytes
コンパイル時間 5,002 ms
コンパイル使用メモリ 79,716 KB
実行使用メモリ 56,640 KB
最終ジャッジ日時 2023-09-14 00:51:51
合計ジャッジ時間 8,646 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 120 ms
55,936 KB
testcase_01 AC 120 ms
55,624 KB
testcase_02 AC 119 ms
55,732 KB
testcase_03 AC 122 ms
55,328 KB
testcase_04 AC 120 ms
55,952 KB
testcase_05 AC 129 ms
56,076 KB
testcase_06 AC 124 ms
55,816 KB
testcase_07 AC 124 ms
55,572 KB
testcase_08 AC 123 ms
55,844 KB
testcase_09 AC 123 ms
53,844 KB
testcase_10 AC 128 ms
55,848 KB
testcase_11 AC 121 ms
55,656 KB
testcase_12 AC 122 ms
55,688 KB
testcase_13 AC 124 ms
55,676 KB
testcase_14 AC 133 ms
55,688 KB
testcase_15 AC 128 ms
55,716 KB
testcase_16 AC 130 ms
56,012 KB
testcase_17 AC 130 ms
53,944 KB
testcase_18 AC 124 ms
55,924 KB
testcase_19 AC 128 ms
56,136 KB
testcase_20 AC 121 ms
55,832 KB
testcase_21 AC 120 ms
56,000 KB
testcase_22 AC 129 ms
55,896 KB
testcase_23 AC 128 ms
56,640 KB
testcase_24 AC 133 ms
55,900 KB
testcase_25 AC 145 ms
55,740 KB
testcase_26 AC 129 ms
55,596 KB
testcase_27 AC 123 ms
55,968 KB
testcase_28 AC 127 ms
55,856 KB
testcase_29 AC 123 ms
55,500 KB
testcase_30 AC 114 ms
55,696 KB
testcase_31 AC 121 ms
55,948 KB
testcase_32 AC 121 ms
55,712 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Queue;
import java.util.Scanner;

public class No3 {

	public static void main(String[]args) {
		Scanner sc = new Scanner(System.in);

		int n = sc.nextInt();
		boolean [] boo = new boolean[n];
		int []m = new int[n];
		Arrays.fill(m, Integer.MAX_VALUE);

		for(int i=0;i<n;i++) {
			boo[i]=false;
		}
		Queue<Integer> q = new ArrayDeque<Integer>();
		boo[0]=true;
		q.add(1);
		m[0]=1;
		while(!q.isEmpty()) {
			int now = q.poll();
			int bitcnt = Integer.bitCount(now);
			int b = now-bitcnt;
			int f = now+bitcnt;
			if(b>0) {
				m[b-1]=Math.min(m[b-1],m[now-1]+1);
				if(!boo[b-1]) {
					q.add(b);
				}
				boo[b-1]=true;
			}
			if(f<=n) {
				m[f-1]=Math.min(m[f-1],m[now-1]+1);
				if(!boo[f-1]) {
					q.add(f);
				}
				boo[f-1]=true;
			}

		}

		if(m[n-1]==Integer.MAX_VALUE) {
			System.out.println(-1);
		}
		else {
			System.out.println(m[n-1]);
		}



		sc.close();

	}



}
0