Skip to content

Instantly share code, notes, and snippets.

@rep-movsd
Last active May 17, 2016 20:56
Show Gist options
  • Select an option

  • Save rep-movsd/69ffd9b244c1edc6864576941078aba9 to your computer and use it in GitHub Desktop.

Select an option

Save rep-movsd/69ffd9b244c1edc6864576941078aba9 to your computer and use it in GitHub Desktop.
Topcoder Inv 2001 Semi A+B 500 pointer (brief version)
#include <bits/stdc++.h>
#define mp make_pair
#define vstr vector<string>
#define pb push_back
#define all(x) begin(x), end(x)
#define sz(x) int(x.size())
#define P(X) cout << X << "\t"
#define NL cout << endl
#define FOR(I,N) for(int I = 0; I < N; ++I)
using namespace std;
typedef pair<int, int> Coord;
typedef pair<int, int> Incr;
static constexpr Incr UP = mp(-1, 0), DN = mp(1, 0), LT = mp(0, -1), RT = mp(0, 1);
static constexpr Incr NE = mp(-1, 1), NW = mp(-1, -1), SE = mp(1, 1), SW = mp(1, -1);
struct Tothello
{
typedef vector<vector<int>> Board;
Board board;
int doCount(const Board &b)
{
int cnt = 0;
FOR(i, 8) cnt += count(all(b[i]), 1);
return cnt;
}
int applyone(Board &b, const Coord &c, const Incr &i1, const Incr &i2)
{
int cnt = 0;
for(Iter2d<int> it(b, c, i1), ite = it.end(); it != ite; ++it)
{
auto it1 = it(i2), ite2 = it1.end();
auto it2 = adjacent_find(it1, ite2, [](int a, int b) {return a==1 && b==0;});
auto it3 = adjacent_find(it2, ite2, [](int a, int b) {return a==0 && b==1;});
if(it2 && it3 && distance(it2,it3) == count(it2, it3.next(), 0))
{
fill(it2, it3.next(), 1);
++cnt;
}
}
return cnt;
}
void apply(Board &b)
{
int cnt;
do
{
cnt = 0;
cnt += applyone(b, mp(0, 0), DN, RT);
cnt += applyone(b, mp(0, 0), RT, DN);
cnt += applyone(b, mp(0, 7), LT, SW);
cnt += applyone(b, mp(1, 7), DN, SW);
cnt += applyone(b, mp(0, 0), RT, SE);
cnt += applyone(b, mp(1, 0), DN, SE);
}
while(cnt);
}
int bestMove(vector<string> red, vector<string> blk, string who)
{
FOR(i, 8) board.pb(vector<int>(8, -1));
int w = (who == "Red" ? 1 : 0);
for(const auto &s:red) board[s[0]-'A'][s[1]-'1'] = w;
for(const auto &s:blk) board[s[0]-'A'][s[1]-'1'] = w^1;
int maxcnt = doCount(board);
FOR(i, 8) FOR(j, 8)
{
if(board[i][j] < 0)
{
Board temp = board;
temp[i][j] = 1;
apply(temp);
maxcnt = max(maxcnt, doCount(temp));
}
}
return maxcnt;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment