結果
| 問題 |
No.3 ビットすごろく
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2015-05-31 17:30:05 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 5,000 ms |
| コード長 | 1,118 bytes |
| コンパイル時間 | 675 ms |
| コンパイル使用メモリ | 67,768 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-07-01 07:22:55 |
| 合計ジャッジ時間 | 1,638 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 33 |
ソースコード
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// 移動数を計算
int getMoveNum(int x) {
int num = 0;
while(x > 0) {
if(x&1==1)
num ++;
x = x >> 1;
}
return num;
}
int main() {
int N;
cin >> N;
vector<bool> flag(N+1, false);
flag[1] = true; // スタート位置にいるので
int cnt = 1;
vector<int> pos;
pos.push_back(1);
while(true) {
// 移動先が無い時
if(pos.size() == 0) {
cnt = -1;
break;
}
// 移動先にNが含まれるとき
else if(find(pos.begin(), pos.end(), N) != pos.end() ) {
break;
}
// 移動
vector<int> tmpPos;
for(int i=0; i<pos.size(); i++) {
int moveNum = getMoveNum(pos[i]);
// 前へ移動
int nextPos = pos[i] + moveNum;
if(nextPos <= N && !flag[nextPos]) {
flag[nextPos] = true;
tmpPos.push_back(nextPos);
}
// 後ろへ移動
nextPos = pos[i] - moveNum;
if(nextPos > 0 && !flag[nextPos]) {
flag[nextPos] = true;
tmpPos.push_back(nextPos);
}
}
pos = tmpPos;
// 移動数をカウント
cnt ++;
}
cout << cnt << endl;
return 0;
}