結果

問題 No.3 ビットすごろく
ユーザー sekiya9311sekiya9311
提出日時 2016-04-13 12:23:52
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,195 bytes
コンパイル時間 741 ms
コンパイル使用メモリ 78,152 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-15 00:39:36
合計ジャッジ時間 1,786 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 WA -
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 2 ms
6,944 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 2 ms
6,944 KB
testcase_22 AC 2 ms
6,940 KB
testcase_23 AC 2 ms
6,940 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 2 ms
6,940 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
    //bool flag[N];
    vector<int> m(N);   //マスごとの移動数(絶対値)
    vector<int> turn(N);   //ターン数
    vector<bool> flag(N);   //訪れたことがあればtrue
    queue<int> q;
    int me=0;
    turn[0]=1;
    q.push(me);
    for(int i=0;i<N;i++)  m[i]=bitCount(i+1);
    while(!q.empty()){
        me=q.front();
        q.pop();
        if(flag[me]==true) continue;
        int canGoPlus=m[me]; //前に進められる数(符号プラス)
        int canGoMinus=-m[me];  //後ろに進められる数(符号マイナス)
        if(!(me+canGoPlus>=N)) {turn[me+canGoPlus]=turn[me]+1; q.push(me+canGoPlus);}
        if(!(me+canGoMinus<0)) {turn[me+canGoMinus]=turn[me]+1; q.push(me+canGoMinus);}
        flag[me]=true;
    }
    if(flag[N-1]==true) cout<<turn[N-1]<<endl;
    else cout<<"-1"<<endl;
}
0