結果

問題 No.3 ビットすごろく
ユーザー mono1977mono1977
提出日時 2016-12-02 11:44:19
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,115 bytes
コンパイル時間 183 ms
コンパイル使用メモリ 26,420 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-18 12:14:42
合計ジャッジ時間 5,443 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 WA -
testcase_02 AC 1 ms
4,384 KB
testcase_03 RE -
testcase_04 WA -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 WA -
testcase_21 AC 1 ms
4,380 KB
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 WA -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 AC 1 ms
4,380 KB
testcase_31 WA -
testcase_32 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#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[100000];
	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\n",back,forward);
		
		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