結果

問題 No.3 ビットすごろく
ユーザー soy
提出日時 2019-08-01 14:37:14
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 10 ms / 5,000 ms
コード長 581 bytes
コンパイル時間 813 ms
コンパイル使用メモリ 77,548 KB
最終ジャッジ日時 2025-01-07 10:26:21
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <queue>

using namespace std;

int count(int a) {
	int b = a;
	int i = 0;
	for (; b != 0; b /= 2) {
		i += b % 2;
	}
	return i;
}



int main() {
	int n;
	cin >> n;
	queue<int> que;
	vector<int> dist(n+3, -1);
	dist[1] = 1;
	que.push(1);
	while (!que.empty()) {
		int p = que.front();
		que.pop();
		int i = count(p);
		if ((p - i) < 1);
		else if (dist[p - i] == -1) {
			que.push(p - i);
			dist[p - i] = dist[p] + 1;
		}
		if ((p + i) > n);
		else if (dist[p + i] == -1) {
			que.push(p + i);
			dist[p + i] = dist[p] + 1;
		}
	}
	cout << dist[n];
}
0