Artifact Content
Not logged in

Artifact 50efd272f7286bd4f6d14c71c1b704f6e05cfc4f


#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;

template<typename T>
struct DP3
{
	int N1, N2, N3;
	vector<T> data;
	DP3(int N1, int N2, int N3, const T& t = T())
		: N1(N1), N2(N2), N3(N3), data(N1*N2*N3, t) { assert(data.size()*sizeof(T)<(1<<26)); }
	T& operator()(int i1, int i2, int i3)
		{ return data[ ((i1*N2)+i2)*N3+i3 ]; }
	void swap(DP3& rhs)
		{ data.swap(rhs.data); }
};

class RectangleAvoidingColoring { public:
	long long count(vector <string> board) 
	{
		int H = board.size();
		int W = board[0].size();
		// o????
		// o????
		// xooxo
		// xoxoo
		// xxooo
		if( H>=5 && W>=5 )
			return 0;
		if( H >= 5 )
			transpose(board, H, W);
		// H <= 4

		int BADPAT = H*(H-1)/2;
		DP3<LL> dp(1<<BADPAT, 1<<BADPAT, W+1);
		dp(0,0,0) = 1;
		for(int i=0; i<W; ++i)
		for(int BlackBad=0; BlackBad<(1<<BADPAT); BlackBad++)
		for(int WhiteBad=0; WhiteBad<(1<<BADPAT); WhiteBad++)
		{
			for(int pat=0; pat<(1<<H); ++pat) {
				int bbb = bb(pat,H);
				int wbb = wb(pat,H);
				if( possible(pat, board, i, H) && !(bbb&BlackBad) && !(wbb&WhiteBad) )
					dp(BlackBad|bbb,WhiteBad|wbb,i+1) += dp(BlackBad,WhiteBad,i);
			}
		}
		LL sum = 0;
		for(int BlackBad=0; BlackBad<(1<<BADPAT); BlackBad++)
		for(int WhiteBad=0; WhiteBad<(1<<BADPAT); WhiteBad++)
			sum += dp(BlackBad,WhiteBad,W);
		return sum;
	}
	bool possible(int pat, const vector<string>& board, int i, int H)
	{
		for(int y=0; y<H; ++y) {
			if( board[y][i]=='B' && !(pat&(1<<y))) return false;
			if( board[y][i]=='W' && (pat&(1<<y))) return false;
		}
		return true;
	}
	int bb(int pat,int H)
	{
		int mask = 0, idx=0;
		for(int i=0; i<H; ++i)
		for(int j=i+1; j<H; ++j,++idx)
			if(pat & (1<<i))
			if(pat & (1<<j))
				mask |= (1<<idx);
		return mask;
	}
	int wb(int pat,int H)
	{
		int mask = 0, idx=0;
		for(int i=0; i<H; ++i)
		for(int j=i+1; j<H; ++j,++idx)
			if(!(pat & (1<<i)))
			if(!(pat & (1<<j)))
				mask |= (1<<idx);
		return mask;
	}
	void transpose(vector<string>& b, int& H, int& W)
	{
		vector<string> bb(W);
		for(int x=0; x<W; ++x)
			for(int y=0; y<H; ++y)
				bb[x] += b[y][x];
		swap(H, W);
		b = bb;
	}
};

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

CASE(0)
	string board_[] = {"??",
 "??"};
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	long long _ = 14LL; 
END
CASE(1)
	string board_[] = {"B?",
 "?B"};
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	long long _ = 3LL; 
END
CASE(2)
	string board_[] = {"WW",
 "WW"};
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	long long _ = 0LL; 
END
CASE(3)
	string board_[] = {"??B??",
 "W???W",
 "??B??"};
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	long long _ = 12LL; 
END
CASE(4)
	string board_[] = {"??",
 "W?",
 "W?",
 "?W",
 "W?"};
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	long long _ = 16LL; 
END
/*
CASE(5)
	string board_[] = {};
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	long long _ = LL; 
END
CASE(6)
	string board_[] = ;
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	long long _ = LL; 
END
*/
}
// END CUT HERE