結果

問題 No.3 ビットすごろく
ユーザー airuai
提出日時 2016-09-14 02:07:24
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 3,333 ms / 5,000 ms
コード長 1,035 bytes
コンパイル時間 656 ms
コンパイル使用メモリ 57,008 KB
実行使用メモリ 81,628 KB
最終ジャッジ日時 2024-07-01 08:05:36
合計ジャッジ時間 28,488 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
using namespace std;

#define MAX 10000000

int q[MAX][2];
int head = 0, tail = 0;

void enq(int p, int kind) {
	q[tail][0] = p;
	q[tail][1] = kind;
	++tail;
	if (tail >= MAX) {
		tail = 0;
	}
}

bool deq(int* p, int* kind) {
	if (head == tail) {
		return false;
	}
	else {
		*p = q[head][0];
		*kind = q[head][1];
		++head;
		if (head >= MAX) {
			head = 0;
		}
		return true;
	}
}

int BinaryCount(int n) {
	int count = 0;
	while (n > 0) {
		count += n % 2;
		n /= 2;
	}
	return count;
}

int main(void) {
	int n;
	cin >> n;
	
	bool* flag = new bool[n];
	for (int i = 0; i < n; ++i) {
		flag[i] = false;
	}

	int posi = 1, hosuu;
	int kind = 1;
	do {
		if (posi == n) {
			cout << kind << endl;
			return 0;
		}

		flag[posi - 1] = true;
		hosuu = BinaryCount(posi);
		if (posi - hosuu > 0 && !flag[posi - 1 - hosuu]) {
			enq(posi - hosuu, kind + 1);
		}
		if (posi + hosuu <= n && !flag[posi - 1 + hosuu]) {
			enq(posi + hosuu, kind + 1);
		}
	} while (deq(&posi, &kind));

	cout << "-1" << endl;

	return 0;
}
0