Artifact Content
Not logged in

Artifact 6fa6be636b9ebdef8e6e109204d7a3e722502a02


#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 <cstring>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

struct UnionFind
{
	vector<int> uf, sz;
	int nc;

	UnionFind(int N): uf(N), sz(N,1), nc(N)
		{ for(int i=0; i<N; ++i) uf[i] = i; }
	int size()
		{ return nc; }
	int Find(int a)
		{ return uf[a]==a ? a : uf[a]=Find(uf[a]); }
	bool Union(int a, int b)
		{
			a = Find(a);
			b = Find(b);
			if( a != b )
			{
				if( sz[a] >= sz[b] ) swap(a, b);
				uf[a] = b;
				sz[b] += sz[a];
				--nc;
			}
			return (a!=b);
		}
};

class HexagonPuzzle { public:
	int theCount(vector <string> board) 
	{
		vector< pair<int,int> > id2pos;
		map<pair<int,int>, int> pos2id;
		int N = board.size();
		for(int i=0; i<N; ++i)
			for(int k=0; k<=i; ++k) {
				id2pos.push_back( make_pair(i,k) );
				pos2id[make_pair(i,k)] = id2pos.size()-1;
			}

		int M = id2pos.size();
		UnionFind uf(M);

		for(int i=0; i<N; ++i)
			for(int k=0; k<=i; ++k)
				if( board[i][k] == '.' )
				{
					if( i+1<N && board[i+1][k]=='.' && board[i+1][k+1]=='.' )
					{
						int a = pos2id[ make_pair(i,k) ];
						int b = pos2id[ make_pair(i+1,k) ];
						int c = pos2id[ make_pair(i+1,k+1) ];
						uf.Union(a,b);
						uf.Union(b,c);
					}
					if( i+1<N && k+1<=i && board[i][k+1]=='.' && board[i+1][k+1]=='.' )
					{
						int a = pos2id[ make_pair(i,k) ];
						int b = pos2id[ make_pair(i,k+1) ];
						int c = pos2id[ make_pair(i+1,k+1) ];
						uf.Union(a,b);
						uf.Union(b,c);
					}
				}

		map<int, int> sc;
		for(int i=0; i<N; ++i)
			for(int k=0; k<=i; ++k)
				if( board[i][k] == '.' )
				{
					int a = pos2id[ make_pair(i,k) ];
					int ra = uf.Find(a);
					sc[ra] = uf.sz[ra];
				}

		static const int MODVAL = 1000000007;
		LL result = 1;
		for(map<int,int>::iterator it=sc.begin(); it!=sc.end(); ++it)
		{
			int n = it->second;
			LL r = 1;
			for(int i=3; i<=n; ++i)
				r = (r*i)%MODVAL;
			result = (result * r) % MODVAL;
		}
		return int(result);
	}
};

// 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 int& Expected, const int& 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(_, HexagonPuzzle().theCount(board));}
int main(){

CASE(0)
	string board_[] = {".",
 ".X",
 "X..",
 ".X.X"}
;
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int _ = 3; 
END
CASE(1)
	string board_[] = {"X"}
;
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int _ = 1; 
END
CASE(2)
	string board_[] = {".",
 "..",
 "...",
 ".X.."}
;
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int _ = 20160; 
END
CASE(3)
	string board_[] = {".",
 "..",
 "XXX",
 "..X.",
 ".X..X",
 "XXXX..",
 "..X.X.X",
 "..X.XX.."}
;
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int _ = 108; 
END
CASE(4)
	string board_[] = {".",
 "..",
 "...",
 "....",
 ".....",
 "......",
 ".......",
 "........"};
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int _ = 261547992; 
END
/*
CASE(5)
	string board_[] = ;
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int _ = ; 
END
CASE(6)
	string board_[] = ;
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int _ = ; 
END
*/
}
// END CUT HERE