結果
| 問題 | No.3 ビットすごろく | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2016-02-18 10:50:08 | 
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 2 ms / 5,000 ms | 
| コード長 | 822 bytes | 
| コンパイル時間 | 1,315 ms | 
| コンパイル使用メモリ | 164,132 KB | 
| 実行使用メモリ | 6,948 KB | 
| 最終ジャッジ日時 | 2024-07-01 07:43:50 | 
| 合計ジャッジ時間 | 2,303 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 33 | 
ソースコード
#include <bits/stdc++.h>
using namespace std;
int getBitNum(int x)
{
	int ret = 0;
	while(x) {
		x &= (x-1);
		++ ret;
	}
	return ret;
}
int main(int argc, char** argv)
{
	int MAX      = 10001;
	int NOSERACH = MAX;
	int route[MAX];
	for (int ii=0; ii<MAX; ++ii) route[ii] = NOSERACH;
	route[1] = 1;	//1~nの範囲しか使わない
	int n; cin >> n;
	queue<int> que;
	que.push(1);
	while(!que.empty()) {
		int pos = que.front(); que.pop();
		int mlen = getBitNum(pos);
		int move[2] = {mlen, -mlen};
		for (int ii=0; ii<2; ++ii) {
			int newPos = pos + move[ii];
			if (newPos<1 || newPos>n)
				continue;
			if (route[newPos] != NOSERACH)
				continue;
			route[newPos] = route[pos] + 1;
			que.push(newPos);
		}
	}
	if (route[n]==NOSERACH)
		cout << -1 << endl;
	else
		cout << (route[n]) << endl;
	return 0;
}
            
            
            
        