結果
| 問題 | No.3 ビットすごろく | 
| コンテスト | |
| ユーザー |  mudbdb | 
| 提出日時 | 2015-06-10 21:22:03 | 
| 言語 | C90 (gcc 12.3.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 1 ms / 5,000 ms | 
| コード長 | 1,825 bytes | 
| コンパイル時間 | 632 ms | 
| コンパイル使用メモリ | 22,656 KB | 
| 実行使用メモリ | 5,376 KB | 
| 最終ジャッジ日時 | 2024-07-01 07:23:29 | 
| 合計ジャッジ時間 | 1,210 ms | 
| ジャッジサーバーID (参考情報) | judge2 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 33 | 
コンパイルメッセージ
main.c: In function ‘main’:
main.c:58:3: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   58 |   scanf("%d", &N);
      |   ^~~~~~~~~~~~~~~
            
            ソースコード
#include <stdio.h>
#include <stdlib.h>
int bit_count(int no)
{
  int count = 0;
  int i;
  for (i=0; no!=0; i++) {
    if (no & 1) count++;
    no >>= 1;
  }
  return count;
}
int *List[2];
int List_in[2];
int List_out[2];
void list_init(int size)
{
  int list_n = 2;
  int *work = (int*)malloc(sizeof(int)*size*list_n);
  int i;
  for (i=0; i<list_n; i++) {
    List[i] = work+size*i;
    List_in[i] = 0;
    List_out[i] = 0;
  }
  return;
}
void list_refresh(int list_no)
{
  List_in[list_no] = 0;
  List_out[list_no] = 0;
  return;
}
void list_in(int list_no, int no)
{
  List[list_no][List_in[list_no]] = no;
  List_in[list_no] ++;
  return;
}
int list_out(int list_no)
{
  int no;
  if (List_out[list_no] < List_in[list_no]) {
    no = List[list_no][List_out[list_no]];
    List_out[list_no] ++;
  } else {
    no = 0;
  }
  return no;
}
int list_size(int list_no)
{
  return List_in[list_no] - List_out[list_no];
}
int main()
{
  int N;
  scanf("%d", &N);
  int i;
  int *visit = (int*)malloc(sizeof(int)*N);
  for (i=0; i<N; i++) visit[i] = 0;
  list_init(N);
  int move = 0;
  int no = 1;
  visit[no - 1] = 1;
  list_in(move%2, no);
  while (1) {
    move++;
    list_refresh(move%2);
    while (no = list_out((move-1)%2)) {
      if (no == N) {
        goto END;
      } else {
        int bit_n = bit_count(no);
        if ((no + bit_n) <= N) {
          if (visit[(no + bit_n) - 1] == 0) {
            visit[(no + bit_n) - 1] = 1;
            list_in(move%2, no + bit_n);
          }
        }
        if (1 <= (no - bit_n)) {
          if (visit[(no - bit_n) - 1] == 0) {
            visit[(no - bit_n) - 1] = 1;
            list_in(move%2, no - bit_n);
          }
        }
      }
    }
    if (!list_size(move%2)) {
      move = -1;
      goto END;
    }
  }
END:
  ;
  printf("%d\n", move);
  return 0;
}
            
            
            
        