結果

問題 No.3 ビットすごろく
コンテスト
ユーザー zuvizudar
提出日時 2016-12-23 02:07:19
言語 C90(gcc15)
(gcc 15.2.0)
コンパイル:
gcc-15 -O2 -std=c90 -DONLINE_JUDGE -o a.out _filename_ -lm
実行:
./a.out
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 746 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 150 ms
コンパイル使用メモリ 26,040 KB
最終ジャッジ日時 2026-02-23 23:58:01
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
main.c: In function 'bfs':
main.c:39:1: error: C++ style comments are not allowed in ISO C90
   39 | //              printf("%d\n",v);
      | ^
main.c:39:1: note: (this will be reported only once per input file)

ソースコード

diff #
raw source code

#include<stdio.h>
#define MAX 10000
#define INF 100000


int n,head=0,tail=0,queue[MAX];
int d[10001];	
int used[10001];	
int push(int x){
	queue[tail]=x;
	tail++;
}
int pop(){
	int x;
	x=queue[head];
	head++;
	return x;
}

int bitcount(int x){
	int i=0;
	while(x){
		i+=x%2;
		x=x/2;
	}
	return i;
}

int bfs(int start){
	int v=1,r;

	push(start);
	d[start]=1;
	used[start]=1;
	while(1){
		if(head==tail)return -1;

		v=pop();	
//		printf("%d\n",v);
		if(v==n)
			return d[v];
		r=bitcount(v);		
		if(v+r<=n&&used[v+r]==0){
			push(v+r);
			d[v+r]=d[v]+1;
			used[v+r]=1;
		}
		if(v-r>=0&&used[v-r]==0){
			push(v-r);
			d[v-r]=d[v]+1;
			used[v-r]=1;
		}
	}
	
}

int main(){
	int start=1;
	scanf("%d",&n);
	printf("%d",bfs(start));
	return 0;
}
0