結果

問題 No.3 ビットすごろく
ユーザー yuppe19 😺
提出日時 2015-08-21 20:03:57
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 895 bytes
コンパイル時間 546 ms
コンパイル使用メモリ 68,316 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-07-01 07:28:59
合計ジャッジ時間 1,527 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:10:15: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   10 |   int n; scanf("%d", &n);
      |          ~~~~~^~~~~~~~~~

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;

class range {private: struct I{int x;int operator*(){return x;}bool operator!=(I& lhs){return x<lhs.x;}void operator++(){++x;}};I i,n;
public:range(int n):i({0}),n({n}){}range(int i,int n):i({i}),n({n}){}I& begin(){return i;}I& end(){return n;}};

int main(void) {
  int n; scanf("%d", &n);
  queue<pair<int, int>> que;
  // 点番号、手数
  que.push(make_pair(1, 1));
  vector<int> memo(n+1, -1);
  int res = -1;
  while(!que.empty()) {
    pair<int, int> cur = que.front(); que.pop();
    int v = cur.first,
        cnt = cur.second;
    if(v == n) { res = cnt; break; }
    if(memo[v] != -1) { continue; }
    memo[v] = cnt;
    int x = __builtin_popcount(v);
    if(v+x <= n) { que.push(make_pair(v+x, cnt+1)); }
    if(v-x >= 1) { que.push(make_pair(v-x, cnt+1)); }
  }
  printf("%d\n", res);
  return 0;
}
0