結果

問題 No.3 ビットすごろく
ユーザー komori3komori3
提出日時 2017-08-21 02:47:28
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,303 bytes
コンパイル時間 536 ms
コンパイル使用メモリ 71,084 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-23 00:55:11
合計ジャッジ時間 6,459 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 1 ms
6,940 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 3 ms
6,944 KB
testcase_08 AC 9 ms
6,944 KB
testcase_09 AC 97 ms
6,940 KB
testcase_10 AC 250 ms
6,944 KB
testcase_11 AC 62 ms
6,940 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 325 ms
6,940 KB
testcase_17 AC 501 ms
6,940 KB
testcase_18 AC 2 ms
6,944 KB
testcase_19 AC 618 ms
6,940 KB
testcase_20 AC 1 ms
6,944 KB
testcase_21 AC 1 ms
6,944 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 612 ms
6,944 KB
testcase_25 AC 538 ms
6,944 KB
testcase_26 AC 2 ms
6,940 KB
testcase_27 AC 3 ms
6,944 KB
testcase_28 AC 308 ms
6,940 KB
testcase_29 AC 64 ms
6,940 KB
testcase_30 AC 1 ms
6,940 KB
testcase_31 AC 1 ms
6,940 KB
testcase_32 AC 40 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <numeric>
#include <stack>
#include <utility>

#define MAX 10000

using namespace std;
typedef long long int lint;
typedef pair<int, int> Pii;
typedef pair<int, int> PII;

int bit(int N) {
	int ret = 0;
	for (int i = 1; i <= (1 << 16); i <<= 1) {
		ret += ((N & i) ? 1 : 0);
	}
	return ret;
}

int hoge(int N)
{
	int pos, step, jump, ret = -1;
	vector<bool> visited(N + 1, false);

	stack<Pii> st;
	
	st.push(Pii(1, 1));
	while (!st.empty()) {
		pos = st.top().first;
		step = st.top().second;
		visited[pos] = true;
		if (pos == N) {
			if (ret < 0 || ret > step)
				ret = step;
			visited[pos] = false;
		}
		st.pop();
		
		jump = bit(pos);
		if (pos + jump <= N)
			if(!visited[pos + jump])
				st.push(Pii(pos + jump, step + 1));
		if (pos - jump > 0)
			if (!visited[pos - jump])
				st.push(Pii(pos - jump, step + 1));
	}

	return ret;
}

int N;
int ans = 99999999;

vector<int> visited(MAX + 1, 99999999);

void rec(int i, int step)
{
	if (i <= 0 || i > N)
		return;
	if (i == N && step < ans) {
		ans = step;
		return;
	}
	else if (visited[i] < step)
		return;
	else
		visited[i] = step;
	
	rec(i + bit(i), step + 1);
	rec(i - bit(i), step + 1);
}

int main()
{
	cin >> N;
	rec(1, 1);
	cout << ans << endl;
}
0