結果

問題 No.3 ビットすごろく
ユーザー kaililitskaililits
提出日時 2018-05-30 22:56:11
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,164 bytes
コンパイル時間 978 ms
コンパイル使用メモリ 84,452 KB
実行使用メモリ 4,500 KB
最終ジャッジ日時 2023-09-12 20:50:05
合計ジャッジ時間 2,910 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <iostream>
#include <queue>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

int dist(int x, int y) { return (x > y) ? (x - y) : (y - x); }

int count(int n) {
  int cnt = 0;
  stringstream ss;
  ss << static_cast<bitset<16> >(n);
  for (auto itr = ss.str().begin(); itr != ss.str().end(); itr++) {
    if (*itr == '1') cnt++;
  }
  return cnt;
}

bool in(vector<int> v, int n) {
  for (auto& var : v) {
    if (var == n) return true;
  }
  return false;
}

int solve(int n) {
  int cnt = 1;
  queue<int> q;
  q.push(1);
  vector<int> v;
  v.push_back(1);  // 行ったことがある数字を覚えておく
  while (!q.empty()) {
    int tmp = q.front();
    q.pop();
    if (tmp == n) return cnt;
    int forward = count(tmp) + tmp;
    int backward = tmp - count(tmp);
    if (!in(v, forward) && (forward <= n)) {
      q.push(forward);
      v.push_back(forward);
    } else if (!in(v, backward) && (backward > 0)) {
      q.push(backward);
      v.push_back(backward);
    }
    cnt++;
  }
  return -1;
}

int main() {
  int n;
  cin >> n;
  cout << solve(n) << endl;
  return 0;
}
0