結果

問題 No.3 ビットすごろく
ユーザー 0yuduki00yuduki0
提出日時 2016-08-26 00:59:56
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,063 bytes
コンパイル時間 457 ms
コンパイル使用メモリ 67,088 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-25 18:03:32
合計ジャッジ時間 1,990 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
using namespace std;

int goal(int m, const int n, const int* grid, int* count) {
  // cout << "\nm " << m << endl;
  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) {
        if (count[i] == 0) {
          count[i] = -5;
          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 = new int[n+1];
  int* count = new int[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;
  }
  for (int i = 0; i <= n; i++) {
    count[i] = 0;
  }
  count[1] = 1;
  // for (int i = 0; i <= n; i++) {
  //   cout << grid[i] << " ";
  // }
  int ans = -1;
  ans = goal(n, n, grid, count);
  cout << ans << endl;
  return 0;
}
0