File Annotation
Not logged in
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: template<typename T>
4fd800b3a8 2011-02-23        kinaba: struct DP3
4fd800b3a8 2011-02-23        kinaba: {
4fd800b3a8 2011-02-23        kinaba: 	int N1, N2, N3;
4fd800b3a8 2011-02-23        kinaba: 	vector<T> data;
4fd800b3a8 2011-02-23        kinaba: 	DP3(int N1, int N2, int N3, const T& t = T())
4fd800b3a8 2011-02-23        kinaba: 		: N1(N1), N2(N2), N3(N3), data(N1*N2*N3, t) { assert(data.size()*sizeof(T)<(1<<26)); }
4fd800b3a8 2011-02-23        kinaba: 	T& operator()(int i1, int i2, int i3)
4fd800b3a8 2011-02-23        kinaba: 		{ return data[ ((i1*N2)+i2)*N3+i3 ]; }
4fd800b3a8 2011-02-23        kinaba: 	void swap(DP3& rhs)
4fd800b3a8 2011-02-23        kinaba: 		{ data.swap(rhs.data); }
4fd800b3a8 2011-02-23        kinaba: };
4fd800b3a8 2011-02-23        kinaba: 
4fd800b3a8 2011-02-23        kinaba: class RectangleAvoidingColoring { public:
4fd800b3a8 2011-02-23        kinaba: 	long long count(vector <string> board)
4fd800b3a8 2011-02-23        kinaba: 	{
4fd800b3a8 2011-02-23        kinaba: 		int H = board.size();
4fd800b3a8 2011-02-23        kinaba: 		int W = board[0].size();
4fd800b3a8 2011-02-23        kinaba: 		// o????
4fd800b3a8 2011-02-23        kinaba: 		// o????
4fd800b3a8 2011-02-23        kinaba: 		// xooxo
4fd800b3a8 2011-02-23        kinaba: 		// xoxoo
4fd800b3a8 2011-02-23        kinaba: 		// xxooo
4fd800b3a8 2011-02-23        kinaba: 		if( H>=5 && W>=5 )
4fd800b3a8 2011-02-23        kinaba: 			return 0;
4fd800b3a8 2011-02-23        kinaba: 		if( H >= 5 )
4fd800b3a8 2011-02-23        kinaba: 			transpose(board, H, W);
4fd800b3a8 2011-02-23        kinaba: 		// H <= 4
4fd800b3a8 2011-02-23        kinaba: 
4fd800b3a8 2011-02-23        kinaba: 		int BADPAT = H*(H-1)/2;
4fd800b3a8 2011-02-23        kinaba: 		DP3<LL> dp(1<<BADPAT, 1<<BADPAT, W+1);
4fd800b3a8 2011-02-23        kinaba: 		dp(0,0,0) = 1;
4fd800b3a8 2011-02-23        kinaba: 		for(int i=0; i<W; ++i)
4fd800b3a8 2011-02-23        kinaba: 		for(int BlackBad=0; BlackBad<(1<<BADPAT); BlackBad++)
4fd800b3a8 2011-02-23        kinaba: 		for(int WhiteBad=0; WhiteBad<(1<<BADPAT); WhiteBad++)
4fd800b3a8 2011-02-23        kinaba: 		{
4fd800b3a8 2011-02-23        kinaba: 			for(int pat=0; pat<(1<<H); ++pat) {
4fd800b3a8 2011-02-23        kinaba: 				int bbb = bb(pat,H);
4fd800b3a8 2011-02-23        kinaba: 				int wbb = wb(pat,H);
4fd800b3a8 2011-02-23        kinaba: 				if( possible(pat, board, i, H) && !(bbb&BlackBad) && !(wbb&WhiteBad) )
4fd800b3a8 2011-02-23        kinaba: 					dp(BlackBad|bbb,WhiteBad|wbb,i+1) += dp(BlackBad,WhiteBad,i);
4fd800b3a8 2011-02-23        kinaba: 			}
4fd800b3a8 2011-02-23        kinaba: 		}
4fd800b3a8 2011-02-23        kinaba: 		LL sum = 0;
4fd800b3a8 2011-02-23        kinaba: 		for(int BlackBad=0; BlackBad<(1<<BADPAT); BlackBad++)
4fd800b3a8 2011-02-23        kinaba: 		for(int WhiteBad=0; WhiteBad<(1<<BADPAT); WhiteBad++)
4fd800b3a8 2011-02-23        kinaba: 			sum += dp(BlackBad,WhiteBad,W);
4fd800b3a8 2011-02-23        kinaba: 		return sum;
4fd800b3a8 2011-02-23        kinaba: 	}
4fd800b3a8 2011-02-23        kinaba: 	bool possible(int pat, const vector<string>& board, int i, int H)
4fd800b3a8 2011-02-23        kinaba: 	{
4fd800b3a8 2011-02-23        kinaba: 		for(int y=0; y<H; ++y) {
4fd800b3a8 2011-02-23        kinaba: 			if( board[y][i]=='B' && !(pat&(1<<y))) return false;
4fd800b3a8 2011-02-23        kinaba: 			if( board[y][i]=='W' && (pat&(1<<y))) return false;
4fd800b3a8 2011-02-23        kinaba: 		}
4fd800b3a8 2011-02-23        kinaba: 		return true;
4fd800b3a8 2011-02-23        kinaba: 	}
4fd800b3a8 2011-02-23        kinaba: 	int bb(int pat,int H)
4fd800b3a8 2011-02-23        kinaba: 	{
4fd800b3a8 2011-02-23        kinaba: 		int mask = 0, idx=0;
4fd800b3a8 2011-02-23        kinaba: 		for(int i=0; i<H; ++i)
4fd800b3a8 2011-02-23        kinaba: 		for(int j=i+1; j<H; ++j,++idx)
4fd800b3a8 2011-02-23        kinaba: 			if(pat & (1<<i))
4fd800b3a8 2011-02-23        kinaba: 			if(pat & (1<<j))
4fd800b3a8 2011-02-23        kinaba: 				mask |= (1<<idx);
4fd800b3a8 2011-02-23        kinaba: 		return mask;
4fd800b3a8 2011-02-23        kinaba: 	}
4fd800b3a8 2011-02-23        kinaba: 	int wb(int pat,int H)
4fd800b3a8 2011-02-23        kinaba: 	{
4fd800b3a8 2011-02-23        kinaba: 		int mask = 0, idx=0;
4fd800b3a8 2011-02-23        kinaba: 		for(int i=0; i<H; ++i)
4fd800b3a8 2011-02-23        kinaba: 		for(int j=i+1; j<H; ++j,++idx)
4fd800b3a8 2011-02-23        kinaba: 			if(!(pat & (1<<i)))
4fd800b3a8 2011-02-23        kinaba: 			if(!(pat & (1<<j)))
4fd800b3a8 2011-02-23        kinaba: 				mask |= (1<<idx);
4fd800b3a8 2011-02-23        kinaba: 		return mask;
4fd800b3a8 2011-02-23        kinaba: 	}
4fd800b3a8 2011-02-23        kinaba: 	void transpose(vector<string>& b, int& H, int& W)
4fd800b3a8 2011-02-23        kinaba: 	{
4fd800b3a8 2011-02-23        kinaba: 		vector<string> bb(W);
4fd800b3a8 2011-02-23        kinaba: 		for(int x=0; x<W; ++x)
4fd800b3a8 2011-02-23        kinaba: 			for(int y=0; y<H; ++y)
4fd800b3a8 2011-02-23        kinaba: 				bb[x] += b[y][x];
4fd800b3a8 2011-02-23        kinaba: 		swap(H, W);
4fd800b3a8 2011-02-23        kinaba: 		b = bb;
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 long long& Expected, const long long& 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(_, RectangleAvoidingColoring().count(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:  "??"};
4fd800b3a8 2011-02-23        kinaba: 	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_));
4fd800b3a8 2011-02-23        kinaba: 	long long _ = 14LL;
4fd800b3a8 2011-02-23        kinaba: END
4fd800b3a8 2011-02-23        kinaba: CASE(1)
4fd800b3a8 2011-02-23        kinaba: 	string board_[] = {"B?",
4fd800b3a8 2011-02-23        kinaba:  "?B"};
4fd800b3a8 2011-02-23        kinaba: 	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_));
4fd800b3a8 2011-02-23        kinaba: 	long long _ = 3LL;
4fd800b3a8 2011-02-23        kinaba: END
4fd800b3a8 2011-02-23        kinaba: CASE(2)
4fd800b3a8 2011-02-23        kinaba: 	string board_[] = {"WW",
4fd800b3a8 2011-02-23        kinaba:  "WW"};
4fd800b3a8 2011-02-23        kinaba: 	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_));
4fd800b3a8 2011-02-23        kinaba: 	long long _ = 0LL;
4fd800b3a8 2011-02-23        kinaba: END
4fd800b3a8 2011-02-23        kinaba: CASE(3)
4fd800b3a8 2011-02-23        kinaba: 	string board_[] = {"??B??",
4fd800b3a8 2011-02-23        kinaba:  "W???W",
4fd800b3a8 2011-02-23        kinaba:  "??B??"};
4fd800b3a8 2011-02-23        kinaba: 	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_));
4fd800b3a8 2011-02-23        kinaba: 	long long _ = 12LL;
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:  "W?",
4fd800b3a8 2011-02-23        kinaba:  "W?",
4fd800b3a8 2011-02-23        kinaba:  "?W",
4fd800b3a8 2011-02-23        kinaba:  "W?"};
4fd800b3a8 2011-02-23        kinaba: 	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_));
4fd800b3a8 2011-02-23        kinaba: 	long long _ = 16LL;
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: 	long long _ = LL;
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: 	long long _ = LL;
4fd800b3a8 2011-02-23        kinaba: END
4fd800b3a8 2011-02-23        kinaba: */
4fd800b3a8 2011-02-23        kinaba: }
4fd800b3a8 2011-02-23        kinaba: // END CUT HERE