結果

問題 No.3 ビットすごろく
ユーザー maruyuki95maruyuki95
提出日時 2020-05-22 22:57:47
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,570 bytes
コンパイル時間 2,546 ms
コンパイル使用メモリ 77,580 KB
実行使用メモリ 59,416 KB
最終ジャッジ日時 2024-04-15 18:15:41
合計ジャッジ時間 8,038 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 126 ms
54,060 KB
testcase_01 AC 124 ms
54,284 KB
testcase_02 AC 114 ms
53,096 KB
testcase_03 AC 149 ms
54,312 KB
testcase_04 AC 128 ms
53,920 KB
testcase_05 AC 149 ms
56,820 KB
testcase_06 AC 143 ms
56,384 KB
testcase_07 AC 150 ms
53,980 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 149 ms
57,092 KB
testcase_13 AC 137 ms
54,488 KB
testcase_14 AC 148 ms
57,268 KB
testcase_15 AC 152 ms
59,200 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 132 ms
53,744 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 127 ms
54,256 KB
testcase_22 AC 147 ms
57,372 KB
testcase_23 AC 149 ms
57,372 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 125 ms
54,244 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 125 ms
54,380 KB
testcase_32 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int arg = sc.nextInt();
		int ret = new Main().execute(arg);
		System.out.println(ret);
	}

	private int execute(int goal) {
		List<Integer> routes = new ArrayList<Integer>();
		return searchRoute(1, goal, routes);
	}

	private int searchRoute(int location, int goal, List<Integer> routes) {
		List<Integer> afterRoutes = new ArrayList<Integer>(routes);
 		afterRoutes.add(location);

		if (location == goal) {
			return afterRoutes.size();
		}

		int binaryTotal = calcurateBinaryTotal(location);

		int locationForward = location + binaryTotal;
		if (canMove(goal, afterRoutes, locationForward)) {
			return searchRoute(locationForward, goal, afterRoutes);
		}

		int locationBackward = location - binaryTotal;
		if (canMove(goal, afterRoutes, locationBackward)){
			 return searchRoute(locationBackward, goal, afterRoutes);
		}

		return -1;
	}

	private boolean canMove(int goal, List<Integer> routes, int location) {
		return 1 <= location && location <= goal && !routes.contains(location);
	}


	/**
	 * 10進数の数値を2進数で表現した時の1のビット数を返す
	 * @param decimalNumber	10進数
	 * @return	2進数で表現した時の1のビット数
	 */
	private int calcurateBinaryTotal(int decimalNumber) {
		int ret = 0;
		int quotient = decimalNumber;
		do {
			ret += quotient % 2;
			quotient = quotient / 2;
		} while (quotient > 0);

		return ret;
	}

}
0