結果
問題 | No.3 ビットすごろく |
ユーザー | ShotaroSuzu |
提出日時 | 2020-05-24 18:24:29 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 142 ms / 5,000 ms |
コード長 | 2,604 bytes |
コンパイル時間 | 2,841 ms |
コンパイル使用メモリ | 78,080 KB |
実行使用メモリ | 58,800 KB |
最終ジャッジ日時 | 2023-09-14 01:44:54 |
合計ジャッジ時間 | 8,210 ms |
ジャッジサーバーID (参考情報) |
judge13 / judge12 |
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 105 ms
56,188 KB |
testcase_01 | AC | 103 ms
55,416 KB |
testcase_02 | AC | 103 ms
56,104 KB |
testcase_03 | AC | 117 ms
53,912 KB |
testcase_04 | AC | 124 ms
56,348 KB |
testcase_05 | AC | 142 ms
56,332 KB |
testcase_06 | AC | 133 ms
55,536 KB |
testcase_07 | AC | 125 ms
56,128 KB |
testcase_08 | AC | 121 ms
56,432 KB |
testcase_09 | AC | 137 ms
56,112 KB |
testcase_10 | AC | 130 ms
56,160 KB |
testcase_11 | AC | 135 ms
56,804 KB |
testcase_12 | AC | 139 ms
56,132 KB |
testcase_13 | AC | 119 ms
55,676 KB |
testcase_14 | AC | 136 ms
56,280 KB |
testcase_15 | AC | 140 ms
58,332 KB |
testcase_16 | AC | 141 ms
58,800 KB |
testcase_17 | AC | 142 ms
58,164 KB |
testcase_18 | AC | 121 ms
57,876 KB |
testcase_19 | AC | 140 ms
58,688 KB |
testcase_20 | AC | 108 ms
55,732 KB |
testcase_21 | AC | 104 ms
56,428 KB |
testcase_22 | AC | 136 ms
58,008 KB |
testcase_23 | AC | 140 ms
58,300 KB |
testcase_24 | AC | 133 ms
58,164 KB |
testcase_25 | AC | 139 ms
58,192 KB |
testcase_26 | AC | 105 ms
55,792 KB |
testcase_27 | AC | 117 ms
56,464 KB |
testcase_28 | AC | 137 ms
58,056 KB |
testcase_29 | AC | 139 ms
55,972 KB |
testcase_30 | AC | 106 ms
56,140 KB |
testcase_31 | AC | 107 ms
55,712 KB |
testcase_32 | AC | 135 ms
56,460 KB |
ソースコード
package yukicoder.level2.bitsugoroku; import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class BitSugorokuRecursive { private Integer goal; public static void main(String[] args) { new BitSugorokuRecursive().execute(); } private void execute() { goal = read(); Integer firstLocation = 1; int minimumMovingNum = calcMinimanMovingNumber(firstLocation); output(minimumMovingNum); } private int calcMinimanMovingNumber(Integer firstLocation) { Set<Integer> presentLocations = new HashSet<>(); presentLocations.add(firstLocation); Set<Integer> pastLocations = new HashSet<>(); pastLocations.add(firstLocation); return goToNext(pastLocations, presentLocations, 1); } private int goToNext(Set<Integer> pastLocations, Set<Integer> presentLocations, int count) { if(presentLocations.isEmpty()) { return -1; } if(presentLocations.contains(goal)) { return count; } Set<Integer> newLocations = new HashSet<Integer>(); for (Integer presentLocation : presentLocations) { int movingDistance = calcMovingDistance(presentLocation); Integer nextForwardLocation = presentLocation + movingDistance; if(canMoveTo(nextForwardLocation, pastLocations)) { newLocations.add(nextForwardLocation); pastLocations.add(nextForwardLocation); } Integer nextBackwordLocation = presentLocation - movingDistance; if(canMoveTo(nextBackwordLocation, pastLocations)) { newLocations.add(nextBackwordLocation); pastLocations.add(nextBackwordLocation); } } return goToNext(pastLocations, newLocations, ++count); } private boolean canMoveTo(Integer location,Set<Integer> pastNumbers) { return 1 <= location && location <= goal && pastNumbers.contains(location) == false; } /** * Moving distance is defined as the number of "1"s of the "location" represented in binary. * This method calc the number of "1"s of the "location" represented in binary. * @param location * @return the number of "1"s. */ private int calcMovingDistance(Integer location) { return countOneNumberInBinaly(location, 0); } private Integer countOneNumberInBinaly(Integer decimal, int count) { if(decimal < 1) { return count; } int base = 2; int digit = 1; while(digit * base <= decimal) { digit *= base; } Integer newDecimal = decimal - digit; return countOneNumberInBinaly(newDecimal, ++count); } private void output(int size) { System.out.println(size); } private int read() { @SuppressWarnings("resource") Scanner sc = new Scanner(System.in); return sc.nextInt(); } }