結果

問題 No.3 ビットすごろく
ユーザー w10y26w10y26
提出日時 2017-04-29 17:41:50
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,456 bytes
コンパイル時間 622 ms
コンパイル使用メモリ 76,996 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-14 00:40:34
合計ジャッジ時間 2,028 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#define ll long long
#define ffor(i,a,b) for (int i=(a);i<(b);i++)
#define rfor(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define rep(i,n) for (int i=0;i<(n);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<cmath>
#include<string>
#include<stack>
#include<queue>
#define SIZE 100001
#define MOD 1000000007
#define INF 100000000
using namespace std;
bool flag[SIZE];
int kaisuu[SIZE];


//bitcount
int bitcount(int n){
	int s=0;
	while(n>0){
			s+=n%2;
			n/=2;
	}
	return s;
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);

	rep(i,SIZE) flag[i] = false;//falseで初期化
	rep(i,SIZE) kaisuu[i] = INF;//kaisuuを最大値で初期化->最小値でどんどん更新
	kaisuu[1] = 1;
	int n;
	cin >> n;
	queue<int> q;
	q.push(1);//startの1を入れる

	while(!q.empty()){
		int now = q.front();//現在地
		q.pop();//値を抜く
		if(flag[now]) continue;//すでに訪れた場所
		flag[now] = true;
		int d = bitcount(now);//1の数を調べる

	//まだ訪れたことがない場所であれば回数を入れて位置をqueueに入れる
	if(1 <= now - d && now - d <=n && flag[now - d] == false){
		kaisuu[now - d] = min(kaisuu[now]+1,kaisuu[now - d]);
		q.push(now - d);
	}
	if(1 <= now + d && now + d <= n && flag[now + d] == false){
		kaisuu[now + d] = min(kaisuu[now]+1,kaisuu[now + d]);
		q.push(now + d);
	}
}
	cout << ((flag[n])? kaisuu[n]:-1)<< endl;



	return 0;
}
0