結果
問題 | No.3 ビットすごろく |
ユーザー | ShotaroSuzu |
提出日時 | 2020-05-24 18:24:29 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 173 ms / 5,000 ms |
コード長 | 2,604 bytes |
コンパイル時間 | 4,080 ms |
コンパイル使用メモリ | 77,960 KB |
実行使用メモリ | 56,788 KB |
最終ジャッジ日時 | 2024-07-01 09:49:36 |
合計ジャッジ時間 | 9,783 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 131 ms
53,832 KB |
testcase_01 | AC | 136 ms
54,080 KB |
testcase_02 | AC | 134 ms
53,952 KB |
testcase_03 | AC | 149 ms
54,152 KB |
testcase_04 | AC | 139 ms
54,180 KB |
testcase_05 | AC | 157 ms
54,084 KB |
testcase_06 | AC | 154 ms
54,092 KB |
testcase_07 | AC | 147 ms
53,928 KB |
testcase_08 | AC | 165 ms
53,784 KB |
testcase_09 | AC | 158 ms
54,276 KB |
testcase_10 | AC | 173 ms
56,788 KB |
testcase_11 | AC | 157 ms
54,220 KB |
testcase_12 | AC | 165 ms
54,256 KB |
testcase_13 | AC | 150 ms
54,136 KB |
testcase_14 | AC | 170 ms
56,432 KB |
testcase_15 | AC | 171 ms
56,576 KB |
testcase_16 | AC | 171 ms
56,508 KB |
testcase_17 | AC | 165 ms
56,536 KB |
testcase_18 | AC | 149 ms
53,776 KB |
testcase_19 | AC | 165 ms
56,584 KB |
testcase_20 | AC | 138 ms
53,876 KB |
testcase_21 | AC | 135 ms
53,792 KB |
testcase_22 | AC | 170 ms
54,224 KB |
testcase_23 | AC | 169 ms
56,584 KB |
testcase_24 | AC | 169 ms
56,608 KB |
testcase_25 | AC | 172 ms
56,736 KB |
testcase_26 | AC | 132 ms
53,704 KB |
testcase_27 | AC | 148 ms
55,628 KB |
testcase_28 | AC | 169 ms
56,500 KB |
testcase_29 | AC | 156 ms
54,368 KB |
testcase_30 | AC | 137 ms
54,200 KB |
testcase_31 | AC | 137 ms
54,184 KB |
testcase_32 | AC | 166 ms
54,036 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(); } }