4fd800b3a8 2011-02-23 kinaba: #include <iostream> 4fd800b3a8 2011-02-23 kinaba: #include <sstream> 4fd800b3a8 2011-02-23 kinaba: #include <iomanip> 4fd800b3a8 2011-02-23 kinaba: #include <vector> 4fd800b3a8 2011-02-23 kinaba: #include <string> 4fd800b3a8 2011-02-23 kinaba: #include <map> 4fd800b3a8 2011-02-23 kinaba: #include <set> 4fd800b3a8 2011-02-23 kinaba: #include <algorithm> 4fd800b3a8 2011-02-23 kinaba: #include <numeric> 4fd800b3a8 2011-02-23 kinaba: #include <iterator> 4fd800b3a8 2011-02-23 kinaba: #include <functional> 4fd800b3a8 2011-02-23 kinaba: #include <complex> 4fd800b3a8 2011-02-23 kinaba: #include <queue> 4fd800b3a8 2011-02-23 kinaba: #include <stack> 4fd800b3a8 2011-02-23 kinaba: #include <cmath> 4fd800b3a8 2011-02-23 kinaba: #include <cassert> 4fd800b3a8 2011-02-23 kinaba: #include <cstring> 4fd800b3a8 2011-02-23 kinaba: using namespace std; 4fd800b3a8 2011-02-23 kinaba: typedef long long LL; 4fd800b3a8 2011-02-23 kinaba: typedef complex<double> CMP; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: struct UnionFind 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: vector<int> uf, sz; 4fd800b3a8 2011-02-23 kinaba: int nc; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: UnionFind(int N): uf(N), sz(N,1), nc(N) 4fd800b3a8 2011-02-23 kinaba: { for(int i=0; i<N; ++i) uf[i] = i; } 4fd800b3a8 2011-02-23 kinaba: int size() 4fd800b3a8 2011-02-23 kinaba: { return nc; } 4fd800b3a8 2011-02-23 kinaba: int Find(int a) 4fd800b3a8 2011-02-23 kinaba: { return uf[a]==a ? a : uf[a]=Find(uf[a]); } 4fd800b3a8 2011-02-23 kinaba: bool Union(int a, int b) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: a = Find(a); 4fd800b3a8 2011-02-23 kinaba: b = Find(b); 4fd800b3a8 2011-02-23 kinaba: if( a != b ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: if( sz[a] >= sz[b] ) swap(a, b); 4fd800b3a8 2011-02-23 kinaba: uf[a] = b; 4fd800b3a8 2011-02-23 kinaba: sz[b] += sz[a]; 4fd800b3a8 2011-02-23 kinaba: --nc; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return (a!=b); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class HexagonPuzzle { public: 4fd800b3a8 2011-02-23 kinaba: int theCount(vector <string> board) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: vector< pair<int,int> > id2pos; 4fd800b3a8 2011-02-23 kinaba: map<pair<int,int>, int> pos2id; 4fd800b3a8 2011-02-23 kinaba: int N = board.size(); 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<N; ++i) 4fd800b3a8 2011-02-23 kinaba: for(int k=0; k<=i; ++k) { 4fd800b3a8 2011-02-23 kinaba: id2pos.push_back( make_pair(i,k) ); 4fd800b3a8 2011-02-23 kinaba: pos2id[make_pair(i,k)] = id2pos.size()-1; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int M = id2pos.size(); 4fd800b3a8 2011-02-23 kinaba: UnionFind uf(M); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<N; ++i) 4fd800b3a8 2011-02-23 kinaba: for(int k=0; k<=i; ++k) 4fd800b3a8 2011-02-23 kinaba: if( board[i][k] == '.' ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: if( i+1<N && board[i+1][k]=='.' && board[i+1][k+1]=='.' ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int a = pos2id[ make_pair(i,k) ]; 4fd800b3a8 2011-02-23 kinaba: int b = pos2id[ make_pair(i+1,k) ]; 4fd800b3a8 2011-02-23 kinaba: int c = pos2id[ make_pair(i+1,k+1) ]; 4fd800b3a8 2011-02-23 kinaba: uf.Union(a,b); 4fd800b3a8 2011-02-23 kinaba: uf.Union(b,c); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: if( i+1<N && k+1<=i && board[i][k+1]=='.' && board[i+1][k+1]=='.' ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int a = pos2id[ make_pair(i,k) ]; 4fd800b3a8 2011-02-23 kinaba: int b = pos2id[ make_pair(i,k+1) ]; 4fd800b3a8 2011-02-23 kinaba: int c = pos2id[ make_pair(i+1,k+1) ]; 4fd800b3a8 2011-02-23 kinaba: uf.Union(a,b); 4fd800b3a8 2011-02-23 kinaba: uf.Union(b,c); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: map<int, int> sc; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<N; ++i) 4fd800b3a8 2011-02-23 kinaba: for(int k=0; k<=i; ++k) 4fd800b3a8 2011-02-23 kinaba: if( board[i][k] == '.' ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int a = pos2id[ make_pair(i,k) ]; 4fd800b3a8 2011-02-23 kinaba: int ra = uf.Find(a); 4fd800b3a8 2011-02-23 kinaba: sc[ra] = uf.sz[ra]; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: static const int MODVAL = 1000000007; 4fd800b3a8 2011-02-23 kinaba: LL result = 1; 4fd800b3a8 2011-02-23 kinaba: for(map<int,int>::iterator it=sc.begin(); it!=sc.end(); ++it) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int n = it->second; 4fd800b3a8 2011-02-23 kinaba: LL r = 1; 4fd800b3a8 2011-02-23 kinaba: for(int i=3; i<=n; ++i) 4fd800b3a8 2011-02-23 kinaba: r = (r*i)%MODVAL; 4fd800b3a8 2011-02-23 kinaba: result = (result * r) % MODVAL; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return int(result); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // BEGIN CUT HERE 4fd800b3a8 2011-02-23 kinaba: #include <ctime> 4fd800b3a8 2011-02-23 kinaba: double start_time; string timer() 4fd800b3a8 2011-02-23 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 4fd800b3a8 2011-02-23 kinaba: { os << "{ "; 4fd800b3a8 2011-02-23 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 4fd800b3a8 2011-02-23 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 4fd800b3a8 2011-02-23 kinaba: void verify_case(const int& Expected, const int& Received) { 4fd800b3a8 2011-02-23 kinaba: bool ok = (Expected == Received); 4fd800b3a8 2011-02-23 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 4fd800b3a8 2011-02-23 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 4fd800b3a8 2011-02-23 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 4fd800b3a8 2011-02-23 kinaba: #define END verify_case(_, HexagonPuzzle().theCount(board));} 4fd800b3a8 2011-02-23 kinaba: int main(){ 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: CASE(0) 4fd800b3a8 2011-02-23 kinaba: string board_[] = {".", 4fd800b3a8 2011-02-23 kinaba: ".X", 4fd800b3a8 2011-02-23 kinaba: "X..", 4fd800b3a8 2011-02-23 kinaba: ".X.X"} 4fd800b3a8 2011-02-23 kinaba: ; 4fd800b3a8 2011-02-23 kinaba: vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 4fd800b3a8 2011-02-23 kinaba: int _ = 3; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(1) 4fd800b3a8 2011-02-23 kinaba: string board_[] = {"X"} 4fd800b3a8 2011-02-23 kinaba: ; 4fd800b3a8 2011-02-23 kinaba: vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 4fd800b3a8 2011-02-23 kinaba: int _ = 1; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(2) 4fd800b3a8 2011-02-23 kinaba: string board_[] = {".", 4fd800b3a8 2011-02-23 kinaba: "..", 4fd800b3a8 2011-02-23 kinaba: "...", 4fd800b3a8 2011-02-23 kinaba: ".X.."} 4fd800b3a8 2011-02-23 kinaba: ; 4fd800b3a8 2011-02-23 kinaba: vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 4fd800b3a8 2011-02-23 kinaba: int _ = 20160; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(3) 4fd800b3a8 2011-02-23 kinaba: string board_[] = {".", 4fd800b3a8 2011-02-23 kinaba: "..", 4fd800b3a8 2011-02-23 kinaba: "XXX", 4fd800b3a8 2011-02-23 kinaba: "..X.", 4fd800b3a8 2011-02-23 kinaba: ".X..X", 4fd800b3a8 2011-02-23 kinaba: "XXXX..", 4fd800b3a8 2011-02-23 kinaba: "..X.X.X", 4fd800b3a8 2011-02-23 kinaba: "..X.XX.."} 4fd800b3a8 2011-02-23 kinaba: ; 4fd800b3a8 2011-02-23 kinaba: vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 4fd800b3a8 2011-02-23 kinaba: int _ = 108; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(4) 4fd800b3a8 2011-02-23 kinaba: string board_[] = {".", 4fd800b3a8 2011-02-23 kinaba: "..", 4fd800b3a8 2011-02-23 kinaba: "...", 4fd800b3a8 2011-02-23 kinaba: "....", 4fd800b3a8 2011-02-23 kinaba: ".....", 4fd800b3a8 2011-02-23 kinaba: "......", 4fd800b3a8 2011-02-23 kinaba: ".......", 4fd800b3a8 2011-02-23 kinaba: "........"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 4fd800b3a8 2011-02-23 kinaba: int _ = 261547992; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: /* 4fd800b3a8 2011-02-23 kinaba: CASE(5) 4fd800b3a8 2011-02-23 kinaba: string board_[] = ; 4fd800b3a8 2011-02-23 kinaba: vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 4fd800b3a8 2011-02-23 kinaba: int _ = ; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(6) 4fd800b3a8 2011-02-23 kinaba: string board_[] = ; 4fd800b3a8 2011-02-23 kinaba: vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 4fd800b3a8 2011-02-23 kinaba: int _ = ; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: */ 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE