Hatena::Grouptopcoder

peryaudoのTopCoder日記

 | 

2016-04-16

EllysCandyGame

| 16:23

#include <vector>
#include <iostream>
#include <numeric>

using namespace std;

class EllysCandyGame {
 public:
  string getWinner(vector<int> sweets) {
    const int ans = F(true, 0, 0, sweets);
    if (ans == 1) {
      return "Elly";
    } else if (ans == 0) { 
      return "Draw";
    } else {
      return "Kris";
    }
  }

  int F(bool is_elly, int escore, int kscore, vector<int>& sweets) {
    if (accumulate(sweets.begin(), sweets.end(), 0) == 0) {
      if (escore == kscore) return 0;
      return escore < kscore ? (!is_elly ? 1 : -1) : (is_elly ? 1 : -1);
    }

    int ans = -1;
    for (int i = 0; i < sweets.size(); ++i) {
      const int cur = sweets[i];
      if (cur == 0) continue;
      sweets[i] = 0;
      if (i - 1 >= 0) sweets[i - 1] *= 2;
      if (i + 1 < sweets.size()) sweets[i + 1] *= 2;

      const int curans = -F(!is_elly, is_elly ? escore + cur : escore, !is_elly ? kscore + cur : kscore, sweets);
      ans = max(ans, curans);

      sweets[i] = cur;
      if (i - 1 >= 0) sweets[i - 1] /= 2;
      if (i + 1 < sweets.size()) sweets[i + 1] /= 2;
      if (ans == 1) break;
    }

    return ans;
  }
};

よくあるゲーム木(?)全探索。Drawがあるのでboolでは結果を持てないことに注意。

 |