339e20815a 2014-10-24 kinaba: #include <iostream> 339e20815a 2014-10-24 kinaba: #include <sstream> 339e20815a 2014-10-24 kinaba: #include <iomanip> 339e20815a 2014-10-24 kinaba: #include <vector> 339e20815a 2014-10-24 kinaba: #include <string> 339e20815a 2014-10-24 kinaba: #include <map> 339e20815a 2014-10-24 kinaba: #include <set> 339e20815a 2014-10-24 kinaba: #include <algorithm> 339e20815a 2014-10-24 kinaba: #include <numeric> 339e20815a 2014-10-24 kinaba: #include <iterator> 339e20815a 2014-10-24 kinaba: #include <functional> 339e20815a 2014-10-24 kinaba: #include <complex> 339e20815a 2014-10-24 kinaba: #include <queue> 339e20815a 2014-10-24 kinaba: #include <stack> 339e20815a 2014-10-24 kinaba: #include <cmath> 339e20815a 2014-10-24 kinaba: #include <cassert> 339e20815a 2014-10-24 kinaba: #include <tuple> 339e20815a 2014-10-24 kinaba: using namespace std; 339e20815a 2014-10-24 kinaba: typedef long long LL; 339e20815a 2014-10-24 kinaba: typedef complex<double> CMP; 339e20815a 2014-10-24 kinaba: 339e20815a 2014-10-24 kinaba: class ConnectingGame { public: 339e20815a 2014-10-24 kinaba: string isValid(vector <string> board) 339e20815a 2014-10-24 kinaba: { 339e20815a 2014-10-24 kinaba: return hasCuttingPaint(board) ? "invalid" : "valid"; 339e20815a 2014-10-24 kinaba: } 339e20815a 2014-10-24 kinaba: 339e20815a 2014-10-24 kinaba: bool hasCuttingPaint(vector<string> board) { 339e20815a 2014-10-24 kinaba: const int H = board.size(); 339e20815a 2014-10-24 kinaba: const int W = board[0].size(); 339e20815a 2014-10-24 kinaba: if(H==1 || W==1) 339e20815a 2014-10-24 kinaba: return false; 339e20815a 2014-10-24 kinaba: 339e20815a 2014-10-24 kinaba: set<char> T,L,R,B; 339e20815a 2014-10-24 kinaba: map<char, set<char>> G; 339e20815a 2014-10-24 kinaba: 339e20815a 2014-10-24 kinaba: for(int y=0; y<H; ++y) 339e20815a 2014-10-24 kinaba: for(int x=0; x<W; ++x) 339e20815a 2014-10-24 kinaba: { 339e20815a 2014-10-24 kinaba: if(y==0) T.insert(board[y][x]); 339e20815a 2014-10-24 kinaba: if(y==H-1) B.insert(board[y][x]); 339e20815a 2014-10-24 kinaba: if(x==0) L.insert(board[y][x]); 339e20815a 2014-10-24 kinaba: if(x==W-1) R.insert(board[y][x]); 339e20815a 2014-10-24 kinaba: 339e20815a 2014-10-24 kinaba: int dy[]={0,0,-1,+1}; 339e20815a 2014-10-24 kinaba: int dx[]={-1,+1,0,0}; 339e20815a 2014-10-24 kinaba: for(int d=0; d<4; ++d) { 339e20815a 2014-10-24 kinaba: int yy=y+dy[d], xx=x+dx[d]; 339e20815a 2014-10-24 kinaba: if(0<=yy&&yy<H&&0<=xx&&xx<W&&board[y][x]!=board[yy][xx]) 339e20815a 2014-10-24 kinaba: G[board[y][x]].insert(board[yy][xx]); 339e20815a 2014-10-24 kinaba: } 339e20815a 2014-10-24 kinaba: } 339e20815a 2014-10-24 kinaba: 339e20815a 2014-10-24 kinaba: // Is there a way to paint nodes in G s.t. 339e20815a 2014-10-24 kinaba: // - all L~G~R paths have at least one Blue node, and 339e20815a 2014-10-24 kinaba: // - all T~G~B paths have at least one Red node? 339e20815a 2014-10-24 kinaba: // |G|<=62. 339e20815a 2014-10-24 kinaba: } 339e20815a 2014-10-24 kinaba: }; 339e20815a 2014-10-24 kinaba: 339e20815a 2014-10-24 kinaba: // BEGIN CUT HERE 339e20815a 2014-10-24 kinaba: #include <ctime> 339e20815a 2014-10-24 kinaba: double start_time; string timer() 339e20815a 2014-10-24 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 339e20815a 2014-10-24 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 339e20815a 2014-10-24 kinaba: { os << "{ "; 339e20815a 2014-10-24 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 339e20815a 2014-10-24 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 339e20815a 2014-10-24 kinaba: void verify_case(const string& Expected, const string& Received) { 339e20815a 2014-10-24 kinaba: bool ok = (Expected == Received); 339e20815a 2014-10-24 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 339e20815a 2014-10-24 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 339e20815a 2014-10-24 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 339e20815a 2014-10-24 kinaba: #define END verify_case(_, ConnectingGame().isValid(board));} 339e20815a 2014-10-24 kinaba: int main(){ 339e20815a 2014-10-24 kinaba: 339e20815a 2014-10-24 kinaba: CASE(0) 339e20815a 2014-10-24 kinaba: string board_[] = {"AAB" 339e20815a 2014-10-24 kinaba: ,"CCD"}; 339e20815a 2014-10-24 kinaba: vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 339e20815a 2014-10-24 kinaba: string _ = "invalid"; 339e20815a 2014-10-24 kinaba: END 339e20815a 2014-10-24 kinaba: CASE(1) 339e20815a 2014-10-24 kinaba: string board_[] = {"AA" 339e20815a 2014-10-24 kinaba: ,"BB"}; 339e20815a 2014-10-24 kinaba: vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 339e20815a 2014-10-24 kinaba: string _ = "valid"; 339e20815a 2014-10-24 kinaba: END 339e20815a 2014-10-24 kinaba: CASE(2) 339e20815a 2014-10-24 kinaba: string board_[] = {"iii" 339e20815a 2014-10-24 kinaba: ,"iwi" 339e20815a 2014-10-24 kinaba: ,"iii"}; 339e20815a 2014-10-24 kinaba: vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 339e20815a 2014-10-24 kinaba: string _ = "valid"; 339e20815a 2014-10-24 kinaba: END 339e20815a 2014-10-24 kinaba: CASE(3) 339e20815a 2014-10-24 kinaba: string board_[] = {"SSnukee" 339e20815a 2014-10-24 kinaba: ,"SKnuthe" 339e20815a 2014-10-24 kinaba: ,"SSSothe"}; 339e20815a 2014-10-24 kinaba: vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 339e20815a 2014-10-24 kinaba: string _ = "invalid"; 339e20815a 2014-10-24 kinaba: END 339e20815a 2014-10-24 kinaba: CASE(4) 339e20815a 2014-10-24 kinaba: string board_[] = {"rng58" 339e20815a 2014-10-24 kinaba: ,"rnggn" 339e20815a 2014-10-24 kinaba: ,"rnnnn"}; 339e20815a 2014-10-24 kinaba: vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 339e20815a 2014-10-24 kinaba: string _ = "valid"; 339e20815a 2014-10-24 kinaba: END 339e20815a 2014-10-24 kinaba: CASE(5) 339e20815a 2014-10-24 kinaba: string board_[] = {"ggssGGGGGG","ggssspGGGG","ggsssppGGG","ggWsspGGGG","gggsspGGiG","g7ssspGGGG","gggsseKK33","gggssssK33","gHgsssHA33","HHHHHHHHHH","HHHHHHHHH6","HHHHHHHHb6"}; 339e20815a 2014-10-24 kinaba: vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 339e20815a 2014-10-24 kinaba: string _ = "invalid"; 339e20815a 2014-10-24 kinaba: END 339e20815a 2014-10-24 kinaba: /* 339e20815a 2014-10-24 kinaba: CASE(6) 339e20815a 2014-10-24 kinaba: string board_[] = ; 339e20815a 2014-10-24 kinaba: vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 339e20815a 2014-10-24 kinaba: string _ = ; 339e20815a 2014-10-24 kinaba: END 339e20815a 2014-10-24 kinaba: CASE(7) 339e20815a 2014-10-24 kinaba: string board_[] = ; 339e20815a 2014-10-24 kinaba: vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 339e20815a 2014-10-24 kinaba: string _ = ; 339e20815a 2014-10-24 kinaba: END 339e20815a 2014-10-24 kinaba: */ 339e20815a 2014-10-24 kinaba: } 339e20815a 2014-10-24 kinaba: // END CUT HERE