結果

問題 No.3 ビットすごろく
ユーザー Ysmr_Ry
提出日時 2015-02-01 22:41:30
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
WA  
実行時間 -
コード長 761 bytes
コンパイル時間 645 ms
コンパイル使用メモリ 64,624 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-23 05:26:39
合計ジャッジ時間 1,679 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 14 WA * 19
権限があれば一括ダウンロードができます

ソースコード

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( used[p.first] )
		{ puts("-1"); return 0; }
		
		used[p.first] = true;

		if( p.first+s <= N && !used[p.first+s] )
			que.push( P( p.first+s, p.second+1 ) );
		if( p.first-s >= 1 && !used[p.first-s] )
			que.push( P( p.first-s, p.second+1 ) );
	}

	puts("-1");

	return 0;
}
0