結果

問題 No.3 ビットすごろく
ユーザー 0yuduki00yuduki0
提出日時 2016-08-26 00:19:59
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 924 bytes
コンパイル時間 501 ms
コンパイル使用メモリ 65,628 KB
実行使用メモリ 390,044 KB
最終ジャッジ日時 2024-04-25 17:01:09
合計ジャッジ時間 6,943 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
using namespace std;

int goal(int m, const int n, const int grid[], int count[]) {
  if (m == 1) return count[1];
  else {
    int c = -1;
    for (int i = 1; i <= n; i++) {
      if ((i + grid[i] == m || i - grid[i] == m) && count[i] >= 0) {
        if (count[i] == 0) c = goal(i, n, grid, count) + 1;
        else if (count[i] > 0) c = count[i] + 1;
        if (c > 0 && (count[m] <= 0 || count[m] > c)) count[m] = c;
      }
    }
    if (count[m] == 0) count[m]--;
    return count[m];
  }
}

int main(int argc, char** argv) {
  int n;
  cin >> n;
  int grid[n+1], count[n+1];
  for (int i = 0; i <= n; i++) {
    int num = i, one = 0;
    while (num > 0) {
      if (num % 2 == 1) one++;  num /= 2;
    }
    grid[i] = one;
  }
  count[0] = 0; count[1] = 1;
  for (int i = 2; i <= n; i++) {
    count[i] = 0;
  }
  int ans = -1;
  ans = goal(n, n, grid, count);
  cout << ans << endl;
  return 0;
}
0