結果

問題 No.3 ビットすごろく
ユーザー pocarist
提出日時 2015-09-01 15:44:08
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 306 ms / 5,000 ms
コード長 680 bytes
コンパイル時間 560 ms
コンパイル使用メモリ 70,588 KB
実行使用メモリ 6,016 KB
最終ジャッジ日時 2024-07-01 07:32:41
合計ジャッジ時間 3,979 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <queue>
#include <vector>
#include <tuple>

using namespace std;

int bitcount(int bits)
{
	int num = 0;
	for (; bits != 0; num++)
		bits &= (bits - 1);
	return num;
}

int main()
{
	int N;
	cin >> N;

	vector<bool> visited(N + 1);
	queue<pair<int, int> > q;

	q.push(make_pair(1, 1));
	int ans = -1;
	while (!q.empty()) {
		int i, cnt;
		tie(i, cnt) = q.front();
		q.pop();
		if (i == N) {
			ans = cnt;
			break;
		}
		visited[i] = true;
		int n = bitcount(i);
		if (i + n <= N && !visited[i + n])
			q.push(make_pair(i + n, cnt + 1));
		if (i - n >= 1 && !visited[i - n])
			q.push(make_pair(i - n, cnt + 1));
	}
	cout << ans << endl;
	return 0;
}
0