結果

問題 No.3 ビットすごろく
ユーザー airuai
提出日時 2016-09-14 01:47:47
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
WA  
実行時間 -
コード長 1,191 bytes
コンパイル時間 670 ms
コンパイル使用メモリ 56,620 KB
実行使用メモリ 13,768 KB
最終ジャッジ日時 2024-11-17 05:18:11
合計ジャッジ時間 114,944 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 4 WA * 11 TLE * 18
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
using namespace std;

struct queue {
	queue* next;
	int p, kind;
};

queue* init() {
	queue* q = new queue;
	q->next = NULL;
	return q;
}

void enq(queue* q, int p, int kind) {
	while (q->next != NULL) {
		q = q->next;
	}
	queue* t = new queue;
	t->next = NULL;
	t->p = p;
	t->kind = kind;
	q->next = t;
}

bool deq(queue* q, int* p, int* kind) {
	if (q->next == NULL) {
		return false;
	}
	else {
		*p = q->next->p;
		*kind = q->next->kind;
		queue* t = q->next;
		q->next = q->next->next;
		delete t;
		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;
	}

	queue* q = init();
	int posi = 1, hosuu;
	int kind = 0;
	do {
		if (posi == n) {
			cout << kind << endl;
		}

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

	cout << "-1" << endl;

	return 0;
}
0