結果

問題 No.3 ビットすごろく
コンテスト
ユーザー mono1977
提出日時 2016-12-02 11:52:32
言語 C++11
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=gnu++11 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
RE  
実行時間 -
コード長 1,148 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 298 ms
コンパイル使用メモリ 41,412 KB
実行使用メモリ 7,976 KB
最終ジャッジ日時 2026-05-21 22:46:21
合計ジャッジ時間 3,427 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 11 WA * 3 RE * 19
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include<stdio.h>

int numOfBits(int n){
	int bits=0;
	do{
		bits += n %2;
		n /= 2;
	}while(n>0);
	
	return bits;
}

int main(){
	int goal;
	scanf("%d",&goal);
	int isPassed[goal+1];
	isPassed[0]=1;
	int q[500000];
	q[0]=1;
	q[1]=-1;
	int q_head=0,q_tail=1;
	int position;
	int back,forward;
	int depth = 1;
	
	do{
		if(q[q_head]==-1){
			depth++;
			q_head++;
			q[q_tail+1]=-1;
			q_tail++;
			continue;
		}
		
		position = q[q_head];
		q_head++;
		
		back = position - numOfBits(position);
		forward = position + numOfBits(position);
		isPassed[position]=1;
		
		//printf("position:%2d depth:%2d\n",position,depth);
		//printf("back:%2d forward:%2d\n",back,forward);
		//printf("q_size: %d\n",q_tail);
		
		if(back==goal || forward==goal){
			printf("%d\n",depth+1);
			break;
		}
		
		if(back>0 && back <=goal && isPassed[back]!=1){
			q[q_tail+1]=back;
			q_tail++;
		}
		
		if(forward>0 && forward <=goal && isPassed[forward]!=1){
			q[q_tail+1]=forward;
			q_tail++;
		}
		
		if(q_head == q_tail){
			printf("-1\n");
			break;
		}

			
	}while(1);
	
	//printf("%d\n",numOfBits(n));
	//printf("%d %d",isPassed[0],isPassed[n]);
	
	return 0;
}
0