Created
May 1, 2015 05:15
-
-
Save iseki-masaya/d605a6a20ce76c95915c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
#include <string> | |
#include <iostream> | |
#include <fstream> | |
#include <sstream> | |
#include <list> | |
#include <algorithm> | |
#include <sstream> | |
#include <set> | |
#include <cmath> | |
#include <map> | |
#include <unordered_map> | |
#include <unordered_set> | |
#include <stack> | |
#include <queue> | |
#include <cstdio> | |
//#include <cstdlib> | |
#include <cstring> | |
#include <numeric> | |
#include <bitset> | |
#include <deque> | |
#include <memory> | |
const long long LINF = (5e17); | |
const int INF = 1000000000; | |
#define EPS 1e-7 | |
const int MOD = 1000000007; | |
using namespace std; | |
const int dx[] = {0,1,-1,1,-1,0}; | |
const int dy[] = {-1,-1,0,0,1,1}; | |
class HexagonalBoard { | |
public: | |
int X,Y; | |
void dfs(int x, int y, vector<string> &board, int c, int &ans) { | |
if (board[y][x] != 'X') | |
return; | |
ans = max(1, ans); | |
board[y][x] = c+'1'; | |
for (int i=0; i<6; ++i) { | |
int cx = x + dx[i]; | |
int cy = y + dy[i]; | |
if ( !(0 <= cx && cx < X && 0 <= cy && cy < Y)) | |
continue; | |
if (board[cy][cx] == 'X') { | |
dfs(cx, cy, board, !c, ans); | |
ans = max(2, ans); | |
} else if (board[cy][cx] == c+'1') { | |
ans = max(3, ans); | |
} | |
} | |
} | |
int minColors(vector <string> board) { | |
Y = (int)board.size(); | |
X = (int)board[0].size(); | |
int ans = 0; | |
for (int x=0; x<X; ++x) { | |
for (int y=0; y<Y; ++y) { | |
if (board[y][x] == 'X') { | |
dfs(x, y, board, 1, ans); | |
} | |
} | |
} | |
return ans; | |
} | |
// BEGIN CUT HERE | |
public: | |
void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); } | |
private: | |
template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } | |
void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } | |
void test_case_0() { string Arr0[] = {"---", | |
"---", | |
"---"} | |
; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 0; verify_case(0, Arg1, minColors(Arg0)); } | |
void test_case_1() { string Arr0[] = {"-X--", | |
"---X", | |
"----", | |
"-X--"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 1; verify_case(1, Arg1, minColors(Arg0)); } | |
void test_case_2() { string Arr0[] = {"XXXX", | |
"---X", | |
"---X", | |
"---X"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 2; verify_case(2, Arg1, minColors(Arg0)); } | |
void test_case_3() { string Arr0[] = {"XX-XX--" | |
,"-XX-XXX" | |
,"X-XX--X" | |
,"X--X-X-" | |
,"XX-X-XX" | |
,"-X-XX-X" | |
,"-XX-XX-"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 3; verify_case(3, Arg1, minColors(Arg0)); } | |
// END CUT HERE | |
}; | |
// BEGIN CUT HERE | |
int main() { | |
HexagonalBoard ___test; | |
___test.run_test(-1); | |
} | |
// END CUT HERE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment