結果

問題 No.3 ビットすごろく
ユーザー willowoooo
提出日時 2016-05-14 03:44:11
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 685 bytes
コンパイル時間 575 ms
コンパイル使用メモリ 65,604 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-07-01 07:52:45
合計ジャッジ時間 1,533 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<queue>
using namespace std;

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

int main() {
	int N;
	int visited[10010] = { 0 };
	cin >> N;
	queue<pair<int,int> > qu;
	qu.push(make_pair(1,1));
	while (1) {
		if (qu.empty())
		{
			cout << -1 << endl;
			break;
		}
		int k = qu.front().first;
		int step = qu.front().second;
		if (k == N) {
			cout << step << endl;
			break;
		}
		qu.pop();
		if (visited[k] == 1)
			continue;
		visited[k] = 1;
		if(k+count(k) <= N)
			qu.push(make_pair(k + count(k), step + 1));
		if(k >= count(k))
			qu.push(make_pair(k - count(k), step + 1));
		
	}
	return 0;
}
0