結果

問題 No.3 ビットすごろく
ユーザー roaiziroroaiziro
提出日時 2015-09-22 06:58:57
言語 C90
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 2,293 bytes
コンパイル時間 197 ms
コンパイル使用メモリ 22,784 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-01 07:33:33
合計ジャッジ時間 1,117 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 0 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 0 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 0 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 1 ms
5,376 KB
testcase_17 AC 1 ms
5,376 KB
testcase_18 AC 0 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 1 ms
5,376 KB
testcase_21 AC 0 ms
5,376 KB
testcase_22 AC 1 ms
5,376 KB
testcase_23 AC 1 ms
5,376 KB
testcase_24 AC 1 ms
5,376 KB
testcase_25 AC 1 ms
5,376 KB
testcase_26 AC 1 ms
5,376 KB
testcase_27 AC 1 ms
5,376 KB
testcase_28 AC 1 ms
5,376 KB
testcase_29 AC 1 ms
5,376 KB
testcase_30 AC 1 ms
5,376 KB
testcase_31 AC 1 ms
5,376 KB
testcase_32 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: In function ‘end’:
main.c:27:9: warning: implicit declaration of function ‘free’ [-Wimplicit-function-declaration]
   27 |         free(h);
      |         ^~~~
main.c:2:1: note: include ‘<stdlib.h>’ or provide a declaration of ‘free’
    1 | #include<stdio.h>
  +++ |+#include <stdlib.h>
    2 | #include<stdio.h>
main.c:27:9: warning: incompatible implicit declaration of built-in function ‘free’ [-Wbuiltin-declaration-mismatch]
   27 |         free(h);
      |         ^~~~
main.c:27:9: note: include ‘<stdlib.h>’ or provide a declaration of ‘free’
main.c: In function ‘newmyq’:
main.c:35:11: warning: implicit declaration of function ‘malloc’ [-Wimplicit-function-declaration]
   35 |     new = malloc(sizeof(struct MYQ));
      |           ^~~~~~
main.c:35:11: note: include ‘<stdlib.h>’ or provide a declaration of ‘malloc’
main.c:35:11: warning: incompatible implicit declaration of built-in function ‘malloc’ [-Wbuiltin-declaration-mismatch]
main.c:35:11: note: include ‘<stdlib.h>’ or provide a declaration of ‘malloc’
main.c: In function ‘main’:
main.c:102:5: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  102 |     scanf("%d", &n);
      |     ^~~~~~~~~~~~~~~

ソースコード

diff #

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

#define N 10001
struct MASU{
    int n, f, k;
}bord[N];

struct MYQ{
    void *current;
    struct MYQ *next;
    struct MYQ *back;
};
struct MYQ *head, *tail;

void ini(void)
{
    head = tail = NULL;
    return;
}

void end(void)
{
    struct MYQ *h, *w;
    for(h = head; h != NULL; h = w){
        w = h->next;
        free(h);
    }
    return;
}

struct MYQ* newmyq(void *current)
{
    struct MYQ *new;
    new = malloc(sizeof(struct MYQ));
    new->next = new->back = NULL;
    new->current = current;
    return(new);
}

void enque(struct MYQ *q)
{
    if(tail == NULL){
        head = tail = q;
    }else{
        q->next = head;
        head->back = q;
        head = q;
    }
    return;
}

struct MYQ* deque(void)
{
    struct MYQ *w;
    
    if(tail == NULL){
        return(NULL);
    }
    w = tail;
    tail = tail->back;
    return(w);
}

void qprint(void)
{
    struct MYQ *h, *t;
    struct MASU *w;

    for(h = head; h != NULL; h = h->next){
        w = h->current;
        printf("%d", w->n);
    }
    printf("\n");

    for(t = tail; t!= NULL; t = t->back){
        w = t->current;
        printf("%d", w->n);
    }
    printf("\n");
}

int bit(int n)
{
    int bit = 0;
    do{
        if(n % 2 == 1){
            bit++;
        }
    }while(n >>= 1);
    return(bit);
}

int main(int argc, const char * argv[])
{
    int n, i, front, back;
    struct MYQ *w;
    struct MASU *b;
    
    ini();
    
    scanf("%d", &n);
    for(i = 1; i <= n; i++){
        bord[i].n = i;
        bord[i].f = 0;
        bord[i].k = 0;
    }
    
    enque(newmyq(&bord[1]));
    bord[1].f = bord[1].k = 1;
    while((w = deque()) != NULL){
        b = w->current;
        front = b->n + bit(b->n);
        if(front <= n && bord[front].f == 0){
            enque(newmyq(&bord[front]));
            bord[front].f = 1;
            bord[front].k = bord[b->n].k + 1;
        }
        
        back  = b->n - bit(b->n);
        if(1 <= back && bord[back].f == 0){
            enque(newmyq(&bord[back]));
            bord[back].f = 1;
            bord[back].k = bord[b->n].k + 1;
        }
        
    }
    
    if(0 < bord[n].k){
        printf("%d\n", bord[n].k);
    }else{
        printf("%d\n", -1);
    }
    
    end();
    
    return 0;
}
0