結果

問題 No.3 ビットすごろく
ユーザー Naoyk1212
提出日時 2017-06-01 19:13:17
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
WA  
実行時間 -
コード長 1,088 bytes
コンパイル時間 701 ms
コンパイル使用メモリ 72,252 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-09-21 21:40:57
合計ジャッジ時間 2,166 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 14 WA * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;

int n;
bool visited[100000];
int step[100000];

bool check(int p) {
	bool ok = true;
	if (visited[p])ok = false;
	if (p < 1)ok = false;
	if (p > n)ok = false;
	return ok;
}

int count(int a) {
	int sum = 0;
	for (int i = 0; (a >> i) > 0; i++) {
		sum += ((a >> i) & 1);
	}
	return sum;
}

bool solve(int p) {
	bool ok = false;
	stack<int> st;
	st.push(p);

	step[1] = 1;
	while (!st.empty()) {
		int np = st.top();
		cerr << np << endl;
		st.pop();
	
		if (np == n)ok = true;
		int npp1 = np + count(np);
		if (check(npp1)) {
			visited[npp1] = true;
			step[npp1] = min(step[npp1], step[np] + 1);
			st.push(npp1);
		}

		npp1 = np - count(np);
		if (check(npp1)) {
			visited[npp1] = true;
			step[npp1] = min(step[npp1], step[np] + 1);
			st.push(npp1);
		}
	}
	return ok;
}

int main() {
	cin >> n;
	for (int i = 0; i < 1e5; i++) {
		visited[i] = false;
		step[i] = 1e9;
	}
	
	cout << (solve(1) ? step[n] : -1) << endl;
}
0