結果

問題 No.3 ビットすごろく
ユーザー akakimidori
提出日時 2016-12-08 00:42:00
言語 C90
(gcc 12.3.0)
結果
AC  
実行時間 1 ms / 5,000 ms
コード長 893 bytes
コンパイル時間 170 ms
コンパイル使用メモリ 21,760 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-07-01 08:11:19
合計ジャッジ時間 1,220 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: In function ‘run’:
main.c:17:3: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   17 |   scanf("%d",&n);
      |   ^~~~~~~~~~~~~~

ソースコード

diff #

#include<stdio.h>
#include<stdlib.h>

#define N 10000

int countBit(unsigned int n){
  int res=0;
  while(n>0){
    res+=n&0x01;
    n=(n>>1);
  }
  return res;
}

void run(void){
  int n;
  scanf("%d",&n);

  int *step;
  step=(int *)malloc(sizeof(int)*(n+1));
  int i;
  for(i=0;i<=n;i++){
    step[i]=-1;
  }
  step[1]=1;

  int *queue;
  int len=1000;
  queue=(int *)malloc(sizeof(int)*len);
  int first=0;
  queue[first]=1;
  int last=1;
  while(step[n]==-1 && first!=last){
    int k=queue[first];
    first=(first+1)%len;
    int d=countBit(k);
    if(k-d>=1 && step[k-d]==-1){
      step[k-d]=step[k]+1;
      queue[last]=k-d;
      last=(last+1)%len;
    }
    if(k+d<=n && step[k+d]==-1){
      step[k+d]=step[k]+1;
      queue[last++]=k+d;
      last=(last+1)%len;
    }
  }
  printf("%d\n",step[n]);
  free(step);
  free(queue);
  return;
}

int main(void){
  run();
  return 0;
}
0