結果

問題 No.3 ビットすごろく
ユーザー goodbaton
提出日時 2014-11-16 14:41:14
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,057 bytes
コンパイル時間 625 ms
コンパイル使用メモリ 72,380 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-07-01 07:06:01
合計ジャッジ時間 1,628 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:18:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   18 |     scanf("%d",&n);
      |     ~~~~~^~~~~~~~~

ソースコード

diff #

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>

using namespace std;

int main(){
    queue<pair<int,int>> q;
    int n,now,mo,bit,k;
    bool sude[10001]={};
    
    scanf("%d",&n);
    
    q.push(make_pair(1,1));
    sude[1]=true;
    
    for(;!q.empty();){
        now = bit =q.front().first;
        k = q.front().second;
        q.pop();
        
        if(now==n){
            printf("%d\n",k);
            return 0;
        }
        
        mo=0;
        while(bit>0){
            mo+=bit%2;
            bit/=2;
        }
        
        if(mo<now)
            if(!sude[now-mo]){
                q.push(make_pair(now-mo,k+1));
                sude[now-mo]=true;
            }
        if(mo+now<=n)
            if(!sude[now+mo]){
                q.push(make_pair(now+mo,k+1));
                sude[now+mo]=true;
            }
    }
    
    printf("-1\n");
    return 0;
}
0