結果

問題 No.3 ビットすごろく
ユーザー CsTarepanda
提出日時 2017-05-03 23:08:50
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 532 ms / 5,000 ms
コード長 767 bytes
コンパイル時間 573 ms
コンパイル使用メモリ 69,320 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-01 08:41:00
合計ジャッジ時間 5,883 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;
int n;
int field[10001];

int cnt1(int n){
  int result = 0;
  while(n){
    if(n & 1) result += 1;
    n >>= 1;
  }
  return result;
}

int solve(int now, int depth){
  if(now < 1 || now > n) return -1;
  if(now == n){
    return depth;
  }
  if(field[now] == -1 || field[now] >= depth) {
    field[now] = depth;
    int s1 = solve(now + cnt1(now), depth + 1);
    int s2 = solve(now - cnt1(now), depth + 1);
    if(s1 == -1) return s2;
    if(s2 == -1) return s1;
    return min(s1, s2);
  }else{
    return -1;
  }
}

int main(){
  cin >> n;
  for(int i = 0; i < n; i++){
    field[i] = -1;
  }
  cout << solve(1, 1) << endl;
}
0