結果

問題 No.3 ビットすごろく
ユーザー Elizack
提出日時 2020-06-30 01:18:02
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 973 bytes
コンパイル時間 683 ms
コンパイル使用メモリ 76,388 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-07-01 09:51:31
合計ジャッジ時間 1,661 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <queue>

using namespace std;

int count_one(int n){
	int count = 0;
	while(n > 0){
		if(n % 2 == 1) count++;
		n /= 2;
	}
	return count;
}

int main(){
	int N;
	cin >> N;
	
	int count = 1;
	
	vector<int> pathed(N,0);
	
	queue<pair<int,int>> q;
	q.push(make_pair(1,1));
	
	while(!q.empty()){

		int now = q.front().first;
		if(now == N) {
			cout << q.front().second;
			return 0;
		}
		int next_go = now + count_one(now);

		int next_back = now - count_one(now);
		
		//cout << "now:" << now << ",count:" << q.front().second << endl;
		
		count = q.front().second;
		count++;
		
		if(next_go == N){
			cout << count << endl;
			return 0;
		}
		

		q.pop();

		if(next_go < N){
			if(pathed.at(next_go) != 1){
				q.push(make_pair(next_go, count));
				pathed.at(next_go) = 1;
			}
		}
		
		if(pathed.at(next_back) != 1){
			q.push(make_pair(next_back, count));
			pathed.at(next_back) = 1;
		}
		
	}
	
	cout << -1 << endl;

}
0