Skip to content

Instantly share code, notes, and snippets.

@akashin
Created October 13, 2013 17:01
Show Gist options
  • Save akashin/6964552 to your computer and use it in GitHub Desktop.
Save akashin/6964552 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string.h>
#include <cstdlib>
#include <unordered_set>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <string.h>
#include <set>
#include <map>
#define REP(i, n) for (int i = 0; i < n; ++i)
#define DB(x) cerr << #x << ": " << x << endl;
using namespace std;
typedef long long LL;
const int MAXN = 29;
int n;
char T[MAXN][MAXN];
bool ready[MAXN][MAXN];
int points[MAXN][MAXN];
int bestPlay(int i, int j, int turn)
{
if (!ready[i][j])
{
int nextTurn = 1 - turn;
int score = 0;
int add = 0;
if (T[i][j] == 'a')
{
add = 1;
}
if (T[i][j] == 'b')
{
add = -1;
}
if (i == n - 1 && j == n - 1)
{
score = 0;
}
else
if (i == n - 1)
{
score = bestPlay(i, j + 1, nextTurn);
}
else
if (j == n - 1)
{
score = bestPlay(i + 1, j, nextTurn);
}
else
{
if (turn == 0)
{
score = max(bestPlay(i, j + 1, nextTurn), bestPlay(i + 1, j, nextTurn));
}
else
{
score = min(bestPlay(i, j + 1, nextTurn), bestPlay(i + 1, j, nextTurn));
}
}
ready[i][j] = true;
points[i][j] = score + add;
}
return points[i][j];
}
int main()
{
/* gen(300000, 1000); */
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#endif
scanf("%d", &n);
gets(T[0]);
for (int i = 0; i < n; ++i)
{
gets(T[i]);
/* cerr << T[i] << endl; */
}
int score = bestPlay(0, 0, 1);
/* DB(score); */
if (score > 0)
{
cout << "FIRST" << endl;
}
else
if (score < 0)
{
cout << "SECOND" << endl;
}
else
{
cout << "DRAW" << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment