Artifact Content
Not logged in

Artifact 2058df9c81213c588a7899dad063397925efb80a


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <tuple>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class PathGame { public:
	string judge(vector <string> board)
	{
		vector<int> s;
		for(int i=0; i<board[0].size(); ++i)
			s.push_back((board[0][i]=='#')+(board[1][i]=='#')*2);
		return firstWins(s) ? "Snuke" : "Sothe";
	}

	bool firstWins(const vector<int>& s)
	{
		memo.assign(s.size()*10, -1);

		vector<int> games;

		int i = 0;
		if(s[0] == 0)
		{
			int e = find_if(s.begin()+i, s.end(), [&](int v){return v!=0;}) - s.begin();
			int L = 0;
			int N = e-i;
			int R = e==s.size() ? 0 : s[e];
			games.push_back(grundy(L,N,R));
			i = e;
		}
		while(i < s.size())
		{
			int L = s[i];
			i = find(s.begin()+i, s.end(), 0) - s.begin();
			int e = find_if(s.begin()+i, s.end(), [&](int v){return v!=0;}) - s.begin();
			int N = e-i;
			int R = e==s.size() ? 0 : s[e];
			games.push_back(grundy(L,N,R));
			i = e;
		}

		return xor_all(games) != 0;
	}

	vector<int> memo;
	int grundy(int L, int N, int R)
	{
		if(N == 0)
			return 0; // no move
		if(N == 1) {
			if(L==0 || R==0 || L==R)
				return 1;
			return 0;
		}

		int key = N*9+L*3+R;
		if(memo[key] >= 0)
			return memo[key];

		vector<int> next;
		for(int i=0; i<N; ++i)
		{
			if(i==0) {
				if(L!=2) next.push_back(grundy(1,N-1,R));
				if(L!=1) next.push_back(grundy(2,N-1,R));
			} else if(i==N-1) {
				if(R!=2) next.push_back(grundy(L,N-1,1));
				if(R!=1) next.push_back(grundy(L,N-1,2));
			} else {
				next.push_back(grundy(L,i,1)^grundy(1,N-i-1,R));
				next.push_back(grundy(L,i,2)^grundy(2,N-i-1,R));
			}
		}
		return memo[key] = mex(next);
	}

	int xor_all(const vector<int>& games)
	{
		int v = 0;
		for(int g: games)
			v ^= g;
		return v;
	}

	int mex(const vector<int>& games)
	{
		set<int> gs(games.begin(), games.end());
		for(int v=0;; ++v)
			if(gs.count(v)==0)
				return v;
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const string& Expected, const string& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, PathGame().judge(board));}
int main(){

CASE(0)
	string board_[] = {"#.."
,"..."};
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	string _ = "Snuke"; 
END
CASE(1)
	string board_[] = {"#"
,"."};
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	string _ = "Sothe"; 
END
CASE(2)
	string board_[] = {"....."
,"..#.."};
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	string _ = "Sothe"; 
END
CASE(3)
	string board_[] = {".#..."
,"....."};
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	string _ = "Snuke"; 
END
CASE(4)
	string board_[] = {".....#..#........##......."
,"..........#..........#...."};
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	string _ = "Snuke"; 
END
/*
CASE(5)
	string board_[] = ;
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	string _ = ; 
END
CASE(6)
	string board_[] = ;
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	string _ = ; 
END
*/
}
// END CUT HERE