結果

問題 No.3 ビットすごろく
ユーザー wahr69
提出日時 2015-09-04 14:19:23
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
WA  
実行時間 -
コード長 772 bytes
コンパイル時間 584 ms
コンパイル使用メモリ 63,520 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-07-18 23:41:17
合計ジャッジ時間 1,572 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 17 WA * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>

using namespace std;

int N;

int bitCount(int n) {
	int count = 0;
	while (n > 0) {
		if (n & 1) count++;
		n = n >> 1;
	}
	return count;
}

int result = 0;
bool found = false;
vector<bool> visit;

void solve(int p) {
	if (found) return;
	
	visit[p] = true;
	++result;

	int bitNum = bitCount(p);
	if (p + bitNum == N) {
		++result;
		found = true;
		return;
	}
	if (p + bitNum < N) {
		if (!visit[p + bitNum]) {
			solve(p + bitNum);
			if (!found) --result;
		}
	}
	if (found) return;
	if (p - bitNum > 0) {
		if (!visit[p - bitNum]) {
			solve(p - bitNum);
			if (!found) --result;
		}
	}
}

int main() {
	cin >> N;
	visit.resize(N + 1);

	solve(1);

	if (!found) {
		cout << -1 << endl;
	} else {
		cout << result << endl;
	}
}
0