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では結果を持てないことに注意。

PalindromeMatrixDiv2

| 12:18

#include <iostream>
#include <string>
#include <vector>
#include <climits>
#include <cassert>

using namespace std;

inline int Inv(int x, int lim) {
  return lim - x - 1;
}

void Print(int x, int lim) {
  for (int i = 0; i < lim; ++i) {
    if (x & (1 << i)) cout<<"1"; else cout<<"0";
  }
  cout<<endl;
}

class PalindromeMatrixDiv2 {
 public:
  int minChange(vector<string> A, int rowCount, int columnCount) {
    int ans = INT_MAX;
    for (int i = 0; i < (1 << A.size()); ++i) {
      if (__builtin_popcount(i) < rowCount) continue;
      for (int j = 0; j < (1 << A[0].size()); ++j) {
        if (__builtin_popcount(j) < columnCount) continue;
        vector<string> ca(A);
        for (int k = 0; k < ca.size(); ++k) {
          for (int l = 0; l < ca[0].size(); ++l) {
            vector<int> maj(2, 0);
            ++maj[ca[k][l] - '0'];
            if (i & (1 << k)) ++maj[ca[k][Inv(l, ca[0].size())] - '0'];
            if (j & (1 << l)) ++maj[ca[Inv(k, ca.size())][l] - '0'];
            if (((j & (1<< l)) && (i & (1 << Inv(k, ca.size())))) ||
                ((i & (1 << k)) && (j & (1 << Inv(l, ca[0].size())))))
              ++maj[ca[Inv(k, ca.size())][Inv(l, ca[0].size())] - '0'];
            char filled = maj[0] < maj[1] ? '1' : '0';
            ca[k][l] = filled;
            if (i & (1 << k)) ca[k][Inv(l, ca[0].size())] = filled;
            if (j & (1 << l)) ca[Inv(k, ca.size())][l] = filled;
            if (((j & (1<< l)) && (i & (1 << Inv(k, ca.size())))) ||
                ((i & (1 << k)) && (j & (1 << Inv(l, ca[0].size())))))
              ca[Inv(k, ca.size())][Inv(l, ca[0].size())] = filled;
          }
        }

        int cur = 0;
        for (int k = 0; k < ca.size(); ++k) {
          for (int l = 0; l < ca[0].size(); ++l) {
            cur += ca[k][l] != A[k][l];
          }
        }
        ans = min(ans, cur);
      }
    }

    return ans;
  }
};

行列が十分に小さいのでpalindromeになる行と列を決め打ちして最小になるものを探す。

コードきたな…

 |