結果

問題 No.3 ビットすごろく
ユーザー agisagis
提出日時 2017-03-31 00:28:46
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,204 bytes
コンパイル時間 983 ms
コンパイル使用メモリ 65,520 KB
実行使用メモリ 12,240 KB
最終ジャッジ日時 2023-09-21 08:59:19
合計ジャッジ時間 13,465 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<queue>

using namespace std;

//二進数変換,移動数カウント
int bit(int n) {	
	int counter=1;
	while (n / 2 != 0) {
		counter = counter + n % 2;
		n = n / 2;
	}
	return counter;
}

int main() {
	int N;
	cin >> N;
	queue<int>que1,que2;	//番号,手数
	int counter=0;
	que1.push(1);
	que2.push(0);
	que2.push(0);
	int flag;
	
	while (1) {
		flag = false;
		if (N == 1) {
			cout << 0 << endl;
			break;
		}

		int n = que1.front();
		//ゴール
		if (n + bit(n) == N) {
			cout << que2.front() + 1 << endl;
			break;
		}
		//すすむ
		else if (n + bit(n) < N) {
			que1.push(n + bit(n));
			que1.pop();
			que2.push(que2.front() + 1);
			que2.pop();
			flag = true;
		}
		//すすまない
		else {
			que2.push(que2.front());
			que2.pop();
		}
		//ゴール
		if (n - bit(n) == N) {
			cout << que2.front() + 1 << endl;
			break;
		}
		//もどる
		else if (n - bit(n) >= 1) {
			que1.push(n - bit(n));
			que1.pop();
			que2.push(que2.front() + 1);
			que2.pop();
			flag = true;
		}
		//もどらない
		else {
			que2.push(que2.front());
			que2.pop();
		}
		//ゴールできない
		if (flag == false){
			cout << -1 << endl;
			break;
		}
	}
	return 0;
}
0