Artifact Content
Not logged in

Artifact 0b43e0ef88516e582ecd3012ea1934fca009921f


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

class SpecificPolyominoCovering
{
public:
	vector <string> findCovering(vector <string> region) 
	{
		try {
			rec(0, 0, region);
		} catch( vector<string>& r ) {
			return r;
		}
		return vector<string>();
	}

	bool nextX(int &y, int &x, vector<string>& r)
	{
		while( r[y][x] != 'X' ) {
			++x;
			if( x == r[y].size() ) {
				x=0, ++y;
				if( y == r.size() )
					return false;
			}
		}
		return true;
	}

	bool canFill( vector<string> r )
	{
		return canFill_rec(0, 0, r);
	}

	bool canFill_rec(int y, int x, vector<string>& r)
	{
		if( !nextX(y, x, r) )
			return true;

		// if it was the pattern fillable both by B and A,
		//     XX?X
		//     XXXX
		// then, if we can fill starting from A it implies ?=X
		// In this case, we must have been able to fill starting
		// from B. Thus, we never need to try A if we've failed
		// filling by B. The "lexicographically first" will by
		// considered elsewhere.

		if( x+1 < r[y].size() && r[y][x+1]=='X' )
		{
			r[y][x] = r[y][x+1] = 'B';
			return canFill_rec(y, x, r);
		}

		if( x+3 < r[y].size() && y+1 < r.size() )
		{
			int py[] = {0,0,1,1,1,1};
			int px[] = {0,3,0,1,2,3};
			for(int i=0; i<6; ++i)
				if( r[y+py[i]][x+px[i]]!='X' )
					return false;
				else
					r[y+py[i]][x+px[i]] = 'A';
			return canFill_rec(y, x, r);
		}

		return false;
	}

	void rec(int y, int x, vector<string>& r)
	{
		if( !nextX(y, x, r) )
			throw r;

		// try to fill by A
		if( x+3 < r[y].size() && y+1 < r.size() )
		{
			int py[] = {0,0,1,1,1,1};
			int px[] = {0,3,0,1,2,3};
			bool bad = false;
			for(int i=0; i<6; ++i)
				if( r[y+py[i]][x+px[i]]!='X' )
					bad = true;
			if( !bad )
			{
				for(int i=0; i<6; ++i)
					r[y+py[i]][x+px[i]] = 'A';
				if( canFill(r) )
					rec(y, x, r);
				for(int i=0; i<6; ++i)
					r[y+py[i]][x+px[i]] = 'X';
			}
		}
		// try to fill by B
		if( x+1 < r[y].size() && r[y][x+1]=='X' )
		{
			r[y][x] = r[y][x+1] = 'B';
			rec(y, x, r);
			r[y][x] = r[y][x+1] = 'X';
		}
	}
};

// 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> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
int verify_case(const vector <string> &Expected, const vector <string> &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: " << print_array(Expected) << endl; cerr << "\tReceived: " << print_array(Received) << endl; } return 0;}

template<int N> struct Case_ { Case_(){start_time=clock();} };
char Test_(...);
int Test_(Case_<0>) {
	string region_[] = {"XXXX",
 "XXXX"};
	  vector <string> region(region_, region_+sizeof(region_)/sizeof(*region_)); 
	string RetVal_[] = {"ABBA", "AAAA" };
	  vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 
	return verify_case(RetVal, SpecificPolyominoCovering().findCovering(region)); }
int Test_(Case_<1>) {
	string region_[] = {"X..XXXX..X",
 "XXXX..XXXX"};
	  vector <string> region(region_, region_+sizeof(region_)/sizeof(*region_)); 
	string RetVal_[] = {"A..ABBA..A", "AAAA..AAAA" };
	  vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 
	return verify_case(RetVal, SpecificPolyominoCovering().findCovering(region)); }
int Test_(Case_<2>) {
	string region_[] = {"XXXXXX",
 "XXXXXX",
 "XXXXXX"};
	  vector <string> region(region_, region_+sizeof(region_)/sizeof(*region_)); 
	string RetVal_[] = {"ABBABB", "AAAABB", "BBBBBB" };
	  vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 
	return verify_case(RetVal, SpecificPolyominoCovering().findCovering(region)); }
int Test_(Case_<3>) {
	string region_[] = {"X..XX",
 "XXXXX"};
	  vector <string> region(region_, region_+sizeof(region_)/sizeof(*region_)); 
	vector <string> RetVal; 
	return verify_case(RetVal, SpecificPolyominoCovering().findCovering(region)); }
int Test_(Case_<4>) {
	string region_[] = {"XXXXXXXXXX",
 "XXXXXXXXXX",
 "XXXXXXXXXX",
 "XXXXX..XXX",
 "XXXXXXXXXX",
 "XXXXXXXXXX",
 "XXXXXXXXXX"};
	  vector <string> region(region_, region_+sizeof(region_)/sizeof(*region_)); 
	string RetVal_[] = {"ABBAABBABB", "AAAAAAAABB", "ABBABBBBBB", "AAAAA..ABB", "ABBAAAAABB", "AAAAABBABB", "BBBBAAAABB" };
	  vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 
	return verify_case(RetVal, SpecificPolyominoCovering().findCovering(region)); }

template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); }
template<>      void Run_<-1>() {}
int main() { Run_<0>(); }
// END CUT HERE