結果

問題 No.3 ビットすごろく
ユーザー yoshi_kyoshi_k
提出日時 2017-09-23 04:21:30
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,719 bytes
コンパイル時間 944 ms
コンパイル使用メモリ 103,340 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-26 06:30:48
合計ジャッジ時間 2,014 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <string>
#include <sstream>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <deque>
#include <complex>
#include <functional>
#include <utility>
#include <iterator>

#define REP(i, n) for(int i = 0; i < (int)(n); ++i)
#define FOR(i, m, n) for(int i = (m); i < (int)(n); ++i)
#define ALL(x) (x).begin(), (x).end()
#define INF 2000000000

#ifdef LOCAL
  #define eprintf(...) fprintf(stdout, __VA_ARGS__)
#else
  #define eprintf(...) 0
#endif

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef unsigned int uint;

int popcount(int n) {
  bitset<sizeof(int) * 8> b(n); 
  return b.count();
}

int main() {
  int n;
  cin >> n;
  vector<bool> visited(n+1, false);
  deque<int> q;
  q.push_back(1);

  bool cangoal = false;
  int count = 0;
  while(!q.empty()) {
    int cur = q.front();
    q.pop_front();
    int pc = popcount(cur);
    eprintf("cur:%d pc:%d\n", cur, pc);
    if(cur + pc == n) {
      eprintf("found! cur:%d goto:%d\n", cur, cur-pc);
      cangoal = true;
      break;
    }
    int back = cur - pc;
    int forward = cur + pc;
    printf("back:%d forward:%d n:%d\n", back, forward, n);
    if(back >= 1 && !visited[back]) {
      eprintf("cur:%d goto:%d\n", cur, back);
      q.push_back(back);
      visited[back] = true;
    }
    if(forward <= n && !visited[forward]) {
      eprintf("cur:%d goto:%d\n", cur, forward);
      q.push_back(forward);
      visited[forward] = true;
    }
    count++;
  }

  if(cangoal) {
    cout << count << endl;
  }
  else {
    cout << -1 << endl;
  }
  return 0;
}
0