結果

問題 No.3 ビットすごろく
ユーザー Bantako
提出日時 2017-07-17 21:41:20
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,002 bytes
コンパイル時間 340 ms
コンパイル使用メモリ 44,416 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-07-01 08:46:41
合計ジャッジ時間 1,310 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:9:1: warning: ISO C++ forbids declaration of ‘main’ with no type [-Wreturn-type]
    9 | main(){
      | ^~~~
main.cpp: In function ‘int main()’:
main.cpp:10:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   10 |     scanf("%d",&N);
      |     ~~~~~^~~~~~~~~

ソースコード

diff #

#include<stdio.h>
#include<queue>
#include<utility>
using namespace std;
int N;
//行ったことがあったら1
int dp[10000];
int getbit(int x);
main(){
    scanf("%d",&N);
    int step = 1;
    int point = 1;
    queue<pair<int,int> > q;
    q.push(make_pair(point,step));
    while(!q.empty()){
//        printf("%d ",point);//
        //ループ確定
        point = q.front().first;
        step = q.front().second;
        q.pop();
        if(point==N){
            break;
        }
        if(dp[point-1]){
            continue;
        }

        if(point + getbit(point) <= N){
        //前にすすめる場合
            dp[point-1] = 1;
            q.push(make_pair(point+getbit(point),step+1));
        }
        if(point - getbit(point) > 0){
        //後ろに進む
            dp[point-1] = 1;
            q.push(make_pair(point-getbit(point),step+1));
        }
    }
    printf("%d",point==N?step:-1);
    return 0;
}

int getbit(int x){
     return __builtin_popcountl(x);
}
0