結果

問題 No.3 ビットすごろく
ユーザー aimBULLaimBULL
提出日時 2016-06-02 00:51:41
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,311 bytes
コンパイル時間 2,198 ms
コンパイル使用メモリ 77,184 KB
実行使用メモリ 41,788 KB
最終ジャッジ日時 2024-04-16 22:57:14
合計ジャッジ時間 7,397 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 124 ms
41,256 KB
testcase_03 AC 126 ms
41,268 KB
testcase_04 WA -
testcase_05 AC 128 ms
41,548 KB
testcase_06 AC 130 ms
41,188 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 123 ms
41,164 KB
testcase_13 AC 126 ms
41,580 KB
testcase_14 AC 127 ms
41,444 KB
testcase_15 AC 126 ms
41,432 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 126 ms
41,648 KB
testcase_23 AC 125 ms
41,644 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 123 ms
41,564 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {

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

		int[] memo = create(n);
		calc(0, create(n));

		System.out.println(memo[n] == Integer.MAX_VALUE ? -1 : memo[n]);
	}

	// データの格納領域を作成する
	public static int[] create(int n){
		int[] memo = new int[n+1];
		memo[0] = Integer.MAX_VALUE;
		memo[1] = 1;
		for(int i = 2; i < memo.length; i++){
			memo[i] = Integer.MAX_VALUE;
		}
		return memo;
	}

	// 移動距離計算する
	// 移動可能なマスの範囲内で移動距離を短縮できる間、再帰的に処理する
	public static void calc(int pos, int[] memo){
		int cnt = Integer.bitCount(pos);

		// 移動した場合の移動距離を計算しておく
		int cost = memo[pos] + 1;

		// 前進
		{
			int nextPos = pos + cnt;
			// 移動数が小さくなるなら、結果を記録して次のマスへ
			if(nextPos < memo.length && cost < memo[nextPos]){
				memo[nextPos] = cost;
				calc(nextPos, memo);
			}
		}

		// 後退
		{
			int prevPos = pos - cnt;
			// 移動数が小さくなるなら、結果を記録して次のマスへ
			if(prevPos > 0 && cost < memo[prevPos]){
				memo[prevPos] = cost;
				calc(prevPos, memo);
			}
		}
	}

}
0