結果

問題 No.3 ビットすごろく
ユーザー silopy
提出日時 2019-06-28 23:56:04
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 710 bytes
コンパイル時間 524 ms
コンパイル使用メモリ 67,816 KB
実行使用メモリ 10,144 KB
最終ジャッジ日時 2024-07-02 05:15:38
合計ジャッジ時間 7,013 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 3 TLE * 1 -- * 29
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<algorithm>
using namespace std;
using lint=int64_t;

int N;
int ans;

int bit[10010];
int bitcnt(int n)
{
	if(bit[n]!=-1)
		return bit[n];
	
	int ret=0;
	while(1)
	{
		if(n==0)break;
		ret+=n%2;
		n/=2;
	}
	bit[n]=ret;
	return ret;
}

bool visited[10010];
void dfs(int d,int cur)
{
	if(cur==N)
	{
		ans=min(ans,d);
		return;
	}
	
	visited[cur]=true;
	int cnt=bitcnt(cur);
	if(cur+cnt<=N && !visited[cur+cnt])
		dfs(d+1,cur+cnt);
	if(1<=cur-cnt && !visited[cur-cnt])
		dfs(d+1,cur-cnt);

	visited[cur]=false;
	return;
}

int main()
{
	ans=1e9;
	for(int i=0;i<10010;i++)
		bit[i]=-1;

	cin >> N;
	dfs(1,1);
	if(ans==1e9)
		cout << -1;
	else
		cout << ans;
	cout << endl;
	return 0;
}
0