結果
| 問題 |
No.3 ビットすごろく
|
| コンテスト | |
| ユーザー |
sekiya9311
|
| 提出日時 | 2016-04-13 12:23:52 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,195 bytes |
| コンパイル時間 | 718 ms |
| コンパイル使用メモリ | 78,616 KB |
| 実行使用メモリ | 6,820 KB |
| 最終ジャッジ日時 | 2024-10-04 07:14:13 |
| 合計ジャッジ時間 | 1,610 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 14 WA * 19 |
ソースコード
#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;
}
sekiya9311