db58c713f6 2012-02-24 kinaba: #include <iostream> db58c713f6 2012-02-24 kinaba: #include <sstream> db58c713f6 2012-02-24 kinaba: #include <iomanip> db58c713f6 2012-02-24 kinaba: #include <vector> db58c713f6 2012-02-24 kinaba: #include <string> db58c713f6 2012-02-24 kinaba: #include <map> db58c713f6 2012-02-24 kinaba: #include <set> db58c713f6 2012-02-24 kinaba: #include <algorithm> db58c713f6 2012-02-24 kinaba: #include <numeric> db58c713f6 2012-02-24 kinaba: #include <iterator> db58c713f6 2012-02-24 kinaba: #include <functional> db58c713f6 2012-02-24 kinaba: #include <complex> db58c713f6 2012-02-24 kinaba: #include <queue> db58c713f6 2012-02-24 kinaba: #include <stack> db58c713f6 2012-02-24 kinaba: #include <cmath> db58c713f6 2012-02-24 kinaba: #include <cassert> db58c713f6 2012-02-24 kinaba: #include <cstring> db58c713f6 2012-02-24 kinaba: #ifdef __GNUC__ db58c713f6 2012-02-24 kinaba: #include <ext/hash_map> db58c713f6 2012-02-24 kinaba: #define unordered_map __gnu_cxx::hash_map db58c713f6 2012-02-24 kinaba: #else db58c713f6 2012-02-24 kinaba: #include <unordered_map> db58c713f6 2012-02-24 kinaba: #endif db58c713f6 2012-02-24 kinaba: using namespace std; db58c713f6 2012-02-24 kinaba: typedef long long LL; db58c713f6 2012-02-24 kinaba: typedef complex<double> CMP; db58c713f6 2012-02-24 kinaba: db58c713f6 2012-02-24 kinaba: class EllysCheckers { public: db58c713f6 2012-02-24 kinaba: string getWinner(string board) db58c713f6 2012-02-24 kinaba: { db58c713f6 2012-02-24 kinaba: int N = board.size()-1; db58c713f6 2012-02-24 kinaba: int mask = 0; db58c713f6 2012-02-24 kinaba: for(int i=0; i<N; ++i) db58c713f6 2012-02-24 kinaba: mask |= (board[i]=='o')<<i; db58c713f6 2012-02-24 kinaba: vector<int> memo(1<<N, -1); db58c713f6 2012-02-24 kinaba: return win(memo, N, mask) ? "YES" : "NO"; db58c713f6 2012-02-24 kinaba: } db58c713f6 2012-02-24 kinaba: db58c713f6 2012-02-24 kinaba: bool win(vector<int>& memo, int N, int mask) db58c713f6 2012-02-24 kinaba: { db58c713f6 2012-02-24 kinaba: mask &= (1<<N)-1; db58c713f6 2012-02-24 kinaba: if( memo[mask] != -1 ) db58c713f6 2012-02-24 kinaba: return memo[mask]; db58c713f6 2012-02-24 kinaba: if( mask == 0 ) // no checker, no move => lose db58c713f6 2012-02-24 kinaba: return 0; db58c713f6 2012-02-24 kinaba: db58c713f6 2012-02-24 kinaba: bool I_win = false; db58c713f6 2012-02-24 kinaba: // enumerate all possible moves db58c713f6 2012-02-24 kinaba: for(int i=0; i<N; ++i) db58c713f6 2012-02-24 kinaba: if( mask & (1<<i) ) { db58c713f6 2012-02-24 kinaba: if( !(mask & (1<<i+1)) ) { // step db58c713f6 2012-02-24 kinaba: int newmask = mask &~ (1<<i) | (1<<i+1); db58c713f6 2012-02-24 kinaba: if( !win(memo, N, newmask) ) db58c713f6 2012-02-24 kinaba: I_win = true; db58c713f6 2012-02-24 kinaba: } db58c713f6 2012-02-24 kinaba: if( (mask & (1<<i+1)) && (mask & (1<<i+2)) && !(mask & (1<<i+3)) ) { // jump db58c713f6 2012-02-24 kinaba: int newmask = mask &~ (1<<i) | (1<<i+3); db58c713f6 2012-02-24 kinaba: if( !win(memo, N, newmask) ) db58c713f6 2012-02-24 kinaba: I_win = true; db58c713f6 2012-02-24 kinaba: } db58c713f6 2012-02-24 kinaba: } db58c713f6 2012-02-24 kinaba: return memo[mask] = I_win; db58c713f6 2012-02-24 kinaba: } db58c713f6 2012-02-24 kinaba: }; db58c713f6 2012-02-24 kinaba: db58c713f6 2012-02-24 kinaba: // BEGIN CUT HERE db58c713f6 2012-02-24 kinaba: #include <ctime> db58c713f6 2012-02-24 kinaba: double start_time; string timer() db58c713f6 2012-02-24 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } db58c713f6 2012-02-24 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) db58c713f6 2012-02-24 kinaba: { os << "{ "; db58c713f6 2012-02-24 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) db58c713f6 2012-02-24 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } db58c713f6 2012-02-24 kinaba: void verify_case(const string& Expected, const string& Received) { db58c713f6 2012-02-24 kinaba: bool ok = (Expected == Received); db58c713f6 2012-02-24 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; db58c713f6 2012-02-24 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } db58c713f6 2012-02-24 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); db58c713f6 2012-02-24 kinaba: #define END verify_case(_, EllysCheckers().getWinner(board));} db58c713f6 2012-02-24 kinaba: int main(){ db58c713f6 2012-02-24 kinaba: db58c713f6 2012-02-24 kinaba: CASE(0) db58c713f6 2012-02-24 kinaba: string board = ".o..."; db58c713f6 2012-02-24 kinaba: string _ = "YES"; db58c713f6 2012-02-24 kinaba: END db58c713f6 2012-02-24 kinaba: CASE(1) db58c713f6 2012-02-24 kinaba: string board = "..o..o"; db58c713f6 2012-02-24 kinaba: string _ = "YES"; db58c713f6 2012-02-24 kinaba: END db58c713f6 2012-02-24 kinaba: CASE(2) db58c713f6 2012-02-24 kinaba: string board = ".o...ooo..oo.."; db58c713f6 2012-02-24 kinaba: string _ = "NO"; db58c713f6 2012-02-24 kinaba: END db58c713f6 2012-02-24 kinaba: CASE(3) db58c713f6 2012-02-24 kinaba: string board = "......o.ooo.o......"; db58c713f6 2012-02-24 kinaba: string _ = "YES"; db58c713f6 2012-02-24 kinaba: END db58c713f6 2012-02-24 kinaba: CASE(4) db58c713f6 2012-02-24 kinaba: string board = ".o..o...o....o.....o"; db58c713f6 2012-02-24 kinaba: string _ = "NO"; db58c713f6 2012-02-24 kinaba: END db58c713f6 2012-02-24 kinaba: CASE(5) db58c713f6 2012-02-24 kinaba: string board = "oooooooooooooooooooo"; db58c713f6 2012-02-24 kinaba: string _ = "?"; db58c713f6 2012-02-24 kinaba: END db58c713f6 2012-02-24 kinaba: CASE(6) db58c713f6 2012-02-24 kinaba: string board = "o.o.o.o.o.o.o.o.o.o."; db58c713f6 2012-02-24 kinaba: string _ = "?"; db58c713f6 2012-02-24 kinaba: END db58c713f6 2012-02-24 kinaba: CASE(7) db58c713f6 2012-02-24 kinaba: string board = "."; db58c713f6 2012-02-24 kinaba: string _ = "NO"; db58c713f6 2012-02-24 kinaba: END db58c713f6 2012-02-24 kinaba: CASE(8) db58c713f6 2012-02-24 kinaba: string board = "."; db58c713f6 2012-02-24 kinaba: string _ = "NO"; db58c713f6 2012-02-24 kinaba: END db58c713f6 2012-02-24 kinaba: CASE(9) db58c713f6 2012-02-24 kinaba: string board = "o."; db58c713f6 2012-02-24 kinaba: string _ = "YES"; db58c713f6 2012-02-24 kinaba: END db58c713f6 2012-02-24 kinaba: db58c713f6 2012-02-24 kinaba: } db58c713f6 2012-02-24 kinaba: // END CUT HERE