結果
| 問題 | No.3 ビットすごろく | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2020-05-22 23:39:32 | 
| 言語 | Java (openjdk 23) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 178 ms / 5,000 ms | 
| コード長 | 1,803 bytes | 
| コンパイル時間 | 2,156 ms | 
| コンパイル使用メモリ | 77,700 KB | 
| 実行使用メモリ | 58,412 KB | 
| 最終ジャッジ日時 | 2024-07-01 09:49:17 | 
| 合計ジャッジ時間 | 8,452 ms | 
| ジャッジサーバーID (参考情報) | judge1 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 33 | 
ソースコード
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
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) {
		Set<Integer> locations = new HashSet<Integer>();
		locations.add(1);
		return search(goal, locations, locations, 1);
	}
	private int search(int goal, Set<Integer> passedLocations, Set<Integer> diffLocations, int times) {
		if (diffLocations.contains(goal)) {
			return times;
		}
		Set<Integer> newLocations = new HashSet<Integer>();
		for (Integer location : diffLocations) {
			int binaryTotal = calcurateBinaryTotal(location);
			int locationForward = location + binaryTotal;
			if (isMovedToNewLocation(goal, passedLocations, locationForward)) {
				newLocations.add(locationForward);
			}
			int locationBackward = location - binaryTotal;
			if (isMovedToNewLocation(goal, passedLocations, locationBackward)) {
				newLocations.add(locationBackward);
			}
		}
		if (newLocations.size() > 0) {
			passedLocations.addAll(newLocations);
			return search(goal, passedLocations, newLocations, ++times);
		}
		return -1;
	}
	private boolean isMovedToNewLocation(int goal, Set<Integer> passedLocations, int location) {
		return 1 <= location && location <= goal && !passedLocations.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;
	}
}
            
            
            
        