結果

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

ソースコード

diff #

// yukicoder 3 (http://yukicoder.me/problems/11)
#include<iostream>
#include<queue>

typedef std::pair<int, int> P;

const int MAX_N = 10000;

int popcnt( int i )
{
	int ret = 0;

	for( ; i; i &= i-1 )
		++ret;

	return ret;
}

std::queue<P> que;
bool used[MAX_N+1];

int main()
{
	int N;
	std::cin >> N;

	que.push( P( 1, 1 ) );
	while( !que.empty() )
	{
		P p = que.front(); que.pop();
		int s = popcnt(p.first);

		if( p.first == N )
		{ printf( "%d\n", p.second ); return 0; }
		
		if( p.first+s <= N && !used[p.first+s] )
			que.push( P( p.first+s, p.second+1 ) ), used[p.first+s] = true;
		if( p.first-s >= 1 && !used[p.first-s] )
			que.push( P( p.first-s, p.second+1 ) ), used[p.first-s] = true;
	}

	puts("-1");

	return 0;
}
0