結果

問題 No.3 ビットすごろく
ユーザー kiwi
提出日時 2017-12-05 16:08:29
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 866 bytes
コンパイル時間 531 ms
コンパイル使用メモリ 63,968 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-01 08:51:55
合計ジャッジ時間 1,485 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:22:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   22 |         scanf("%d",&N);
      |         ~~~~~^~~~~~~~~

ソースコード

diff #

//test.cpp

#include <utility>
#include <vector>
#include <iostream>
#include <queue>
#define REP(i,n) for(int i=0;i<=n;i++)
using namespace std;
typedef unsigned long ul;
typedef pair<ul, ul> P;
int bit(int i){
	int cnt=0;
	while(i>0){
		cnt += i%2;
		i /= 2;
	}
	return cnt;
}
int main() {
	int N,INF = 10001,cnt = -1;
	int sg[10000],ms[10001];
	scanf("%d",&N);
	REP(i,N){sg[i] = bit(i);ms[i]=INF;}

	queue<int> q;
	q.push(1);
	ms[1] = 1;
	while (!q.empty()) {
		int now = q.front();
		if(now == N){

			cnt = ms[now];
			break;
		}

		if(0 < now - sg[now] && ms[now - sg[now]] == INF){
			q.push(now - sg[now]);
			ms[now - sg[now]] = ms[now] + 1;
		}
		if(now + sg[now]  <= INF && ms[now + sg[now]] == INF){
			q.push(now + sg[now]);
			ms[now + sg[now]] = ms[now] + 1;
		}
		q.pop();
	}
	if(cnt != -1)printf("%d\n", cnt);
	else printf("-1\n");
    return 0;

}
0