結果

問題 No.3 ビットすごろく
ユーザー KotRmKotRm
提出日時 2019-03-15 19:43:22
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,515 bytes
コンパイル時間 498 ms
コンパイル使用メモリ 62,884 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-14 12:38:31
合計ジャッジ時間 2,095 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <string>
#include <queue>
#define print(x) cout << x << endl;
using namespace std;

queue<int>Q;//キュー
bool visited[10001]; //訪問済みチェック
int counter[10001]; //あるマス目までの最短移動数

/*ビットカウント*/
int BitNum(int bits)
{
    bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555);
    bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333);
    bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f);
    bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff);
    return (bits & 0x0000ffff) + (bits >>16 & 0x0000ffff);
}


int main(void){
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int N;
    cin >> N;
    
    int i, mov, next, c;
    visited[1] = true;
    counter[1] = 1;
    counter[N] = -1;
    Q.push(1);
    /*幅優先探索*/
    while(!Q.empty()){
        i = Q.front();
        Q.pop();
        c = counter[i];
        mov = BitNum(i);

        if((i+mov) == N){
            next = i + mov;
            visited[next] = true;
            counter[next] = c + 1;
            break;
        }
        
        if(!visited[i+mov] && (i+mov) < N){
            next = i + mov;
            Q.push(next);
            visited[next] = true;
            counter[next] = c + 1;
        }
        
        if(!visited[i-mov] && (i-mov) > 1){
            next = i - mov;
            Q.push(next);
            visited[next] = true;
            counter[next] = c + 1;
        }
        
    }
    print(counter[N]);
    return 0;
}
0