結果

問題 No.3 ビットすごろく
ユーザー OnjuOnju
提出日時 2015-01-31 05:42:42
言語 C++11
(gcc 13.3.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 714 bytes
コンパイル時間 420 ms
コンパイル使用メモリ 61,912 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-23 04:31:44
合計ジャッジ時間 2,555 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 32 WA * 1
権限があれば一括ダウンロードができます

ソースコード

diff #

//No.3 ビットすごろく

#include <iostream>
#include <unordered_set>
#include <limits.h>
#include <bitset>

#define NMAX 10000

using namespace std;

int C[NMAX];
int N;

int cal(int val, int cost)
{
	++cost;

	bitset<32> bs(val);
	int count = bs.count();

	int next_l = val - count;
	int next_r = val + count;

	if (next_l > 0)
		if (cost < C[next_l - 1])
		{
			C[next_l - 1] = cost;
			cal(next_l, cost);
		}
	if (next_r <= N)
		if (cost < C[next_r - 1])
		{
			C[next_r - 1] = cost;
			cal(next_r, cost);
		}

	return val;
}

int solve()
{
	cal(1, 1);

	return C[N - 1] < INT_MAX ? C[N - 1] : -1;
}

int main()
{
	cin >> N;

	for (int i = 0; i < N; ++i)
		C[i] = INT_MAX;

	cout << solve();

	return 0;
}
0