結果

問題 No.3 ビットすごろく
ユーザー komori3
提出日時 2017-08-21 03:49:07
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 913 bytes
コンパイル時間 685 ms
コンパイル使用メモリ 67,680 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-07-01 08:48:30
合計ジャッジ時間 1,659 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <numeric>
#include <stack>
#include <utility>
#include <queue>

using namespace std;
typedef long long int lint;

int bit(int N) {
	int ret = 0;
	for (int i = 1; i <= (1 << 16); i <<= 1) {
		ret += ((N & i) ? 1 : 0);
	}
	return ret;
}

int game(int N)
{
	int pos, step, jump;
	vector<int> visit(N + 1, 0);
	queue<int> qu;

	qu.push(1);
	visit[1] = 1;

	while (!qu.empty()) {
		pos = qu.front();
		step = visit[pos];
		qu.pop();

		if (pos == N) {
			return step;
		}

		jump = bit(pos);
		if (pos + jump <= N) {
			if (visit[pos + jump] == 0) {
				qu.push(pos + jump);
				visit[pos + jump] = step + 1;
			}
		}
		if (pos - jump > 0) {
			if (visit[pos - jump] == 0) {
				qu.push(pos - jump);
				visit[pos - jump] = step + 1;
			}
		}
	}

	return -1;
}

int main()
{
	int N;
	cin >> N;
	cout << game(N) << endl;
	return 0;
}
0