結果
| 問題 | No.3 ビットすごろく | 
| コンテスト | |
| ユーザー |  sekiya9311 | 
| 提出日時 | 2016-04-13 16:30:06 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 2 ms / 5,000 ms | 
| コード長 | 1,165 bytes | 
| コンパイル時間 | 788 ms | 
| コンパイル使用メモリ | 78,404 KB | 
| 実行使用メモリ | 6,944 KB | 
| 最終ジャッジ日時 | 2024-07-01 07:49:39 | 
| 合計ジャッジ時間 | 1,720 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 33 | 
ソースコード
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int bitCount(int a){
    a=(a&0x55555555)+(a>>1&0x55555555);
    a=(a&0x33333333)+(a>>2&0x33333333);
    a=(a&0x0f0f0f0f)+(a>>4&0x0f0f0f0f);
    a=(a&0x00ff00ff)+(a>>8&0x00ff00ff);
    a=(a&0x0000ffff)+(a>>16&0x0000ffff);
    return a;
}
int main(void){
    int N;
    cin>>N;
    vector<int> turn(N+1,0);   //ターン数
    vector<bool> flag(N+1,false);   //訪れたことがあればtrue
    queue<int> q;
    turn[1]=1;
    q.push(1);
    while(!q.empty()){
        int me=q.front(); q.pop();   //今いる場所
        if(flag[me]) continue; //訪れていればcontinue
        int next=bitCount(me); //移動数計算
        int canGoPlus=me+next; //前に進んだ場合の次のマス
        int canGoMinus=me-next;  //後ろに進んだ場合の次のマス
        if(!(canGoPlus>N) && !turn[canGoPlus]) {turn[canGoPlus]=turn[me]+1; q.push(canGoPlus);}
        if(!(canGoMinus<1) && !turn[canGoMinus]) {turn[canGoMinus]=turn[me]+1; q.push(canGoMinus);}
        flag[me]=true;  //今いる場所に訪れた印
    }
    if(!turn[N]) turn[N]--;
    cout<<turn[N]<<endl;
}
            
            
            
        