結果

問題 No.3 ビットすごろく
ユーザー yuma220284
提出日時 2019-07-21 10:21:50
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 612 bytes
コンパイル時間 1,289 ms
コンパイル使用メモリ 163,888 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-07-01 09:25:59
合計ジャッジ時間 2,257 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;

int N, Dist[10010], INF = 1000000000;

int main() {
	cin >> N;
	for (int i = 1; i <= N; i++) Dist[i] = INF;
	Dist[1] = 1;
	queue<int> Q;
	Q.push(1);
	while (!Q.empty()) {
		int X = Q.front();
		Q.pop();
		if (X == N) {
			cout << Dist[X] << endl;
			return 0;
		}
		int _X= X, Y = 0;
		while (_X != 0) Y += _X % 2, _X /= 2;
		if (X - Y >= 1) {
			if (Dist[X - Y] == INF) {
				Dist[X - Y] = Dist[X] + 1;
				Q.push(X - Y);
			}
		}
		if (X + Y <= N) {
			if (Dist[X + Y] == INF) {
				Dist[X + Y] = Dist[X] + 1;
				Q.push(X + Y);
			}
		}
	}
	cout << -1 << endl;
}
0