結果

問題 No.3 ビットすごろく
コンテスト
ユーザー hanorver
提出日時 2016-05-18 09:54:01
言語 C++11
(gcc 15.2.0 + boost 1.90.0)
コンパイル:
g++-15 -O2 -lm -std=gnu++11 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 1 ms / 5,000 ms
+ 520µs
コード長 810 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 466 ms
コンパイル使用メモリ 90,684 KB
実行使用メモリ 5,888 KB
最終ジャッジ日時 2026-08-03 03:36:01
合計ジャッジ時間 1,930 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>

int bit_num(int num) {
	int count = 0;
	while (num != 0) {
		if (num % 2 == 1) count++;
		num /= 2;
	}
	return count;
}


int main() {
	int n;
	std::cin >> n;

	const int INF = 9999999;

	std::vector<int> masu(n + 1);
	for (int i = 0; i < masu.size(); i++) masu[i] = INF;
	masu[1] = 1;
	std::queue<int> s;
	s.push(1);

	while(!s.empty()) {
		int pos = s.front();
		s.pop();
		int bit = bit_num(pos);
		if (pos + bit < masu.size() && masu[pos + bit] > masu[pos] + 1) {
			masu[pos + bit] = masu[pos] + 1;
			s.push(pos + bit);
		}
		if (pos - bit > 0 && masu[pos - bit] > masu[pos] + 1){
			masu[pos - bit] = masu[pos] + 1;
			s.push(pos - bit);
		}
	}

	if (masu[n] == INF) masu[n] = -1;

	std::cout << masu[n] << std::endl;
	return 0;
}
0