結果

問題 No.3 ビットすごろく
ユーザー nanatutmcnanatutmc
提出日時 2019-09-28 01:17:12
言語 C
(gcc 12.3.0)
結果
WA  
実行時間 -
コード長 1,007 bytes
コンパイル時間 574 ms
コンパイル使用メモリ 29,792 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-25 09:16:08
合計ジャッジ時間 2,229 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 WA -
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 WA -
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 WA -
testcase_11 AC 1 ms
4,348 KB
testcase_12 AC 1 ms
4,348 KB
testcase_13 AC 1 ms
4,348 KB
testcase_14 AC 1 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 1 ms
4,348 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 1 ms
4,348 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 1 ms
4,348 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 AC 1 ms
4,348 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c:41:1: warning: return type defaults to 'int' [-Wimplicit-int]
   41 | main() {
      | ^~~~

ソースコード

diff #

#include <stdio.h>

int N;
int flags[10000];

int bits(int position) {
    int b = 0;
    while (position > 0) {
        if ((position&1) > 0)
            b++;
        position = position >> 1;
    }
    return b;
}

int move(int position, int step) {
    int b = bits(position);
    int fpos = -1, bpos = -1;
    
    flags[position] = 1;
    
    if (position+b == N)
        return step+1;

    if (position+b < N && flags[position+b] == 0)
        fpos = move(position+b, step+1);
    if (position-b > 0 && flags[position-b] == 0)
        bpos = move(position-b, step+1);
    
    if (fpos == -1) {
        return bpos;
    } else {
        if (bpos == -1)
            return fpos;
        else if (fpos < bpos)
            return fpos;
        else
            return bpos;
    }
}
main() {
    scanf("%d", &N);

    if (N == 1) {
        printf("0\n");
        return 0;
    }
    
    for (int i=0; i<10000; i++)
        flags[i] = 0;

    int result = move(1, 0);
    
    printf("%d\n", result);
}
0