Artifact Content
Not logged in

Artifact 76ef41649a38104014a20ffd145eb336cee24858


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

class ThreeColorability { public:
	vector <string> lexSmallest(vector <string> cells)
	{
		if(!can(cells))
			return vector<string>();

		for(int y=0; y<cells.size(); ++y)
		for(int x=0; x<cells[y].size(); ++x)
			if(cells[y][x] == '?')
			{
				cells[y][x] = 'N';
				if(!can(cells))
					cells[y][x] = 'Z';
			}
		return cells;
	}

	enum {R,G,B};
	enum {RG,RB,GR,GB,BR,BG};
	bool can(const vector<string>& C)
	{
		int N = C[0].size();
		vector<int> P(N, (1<<6)-1);

		for(int y=0; y<C.size(); ++y) {
			apply(P, C[y]);

			int can = (1<<R)|(1<<G)|(1<<B);
			for(int x=0; x<N; ++x)
			{
				int can2 = 0;
				if((can&(1<<R))&&(P[x]&(1<<RG))) can2 |= 1<<G;
				if((can&(1<<R))&&(P[x]&(1<<RB))) can2 |= 1<<B;
				if((can&(1<<G))&&(P[x]&(1<<GR))) can2 |= 1<<R;
				if((can&(1<<G))&&(P[x]&(1<<GB))) can2 |= 1<<B;
				if((can&(1<<B))&&(P[x]&(1<<BR))) can2 |= 1<<R;
				if((can&(1<<B))&&(P[x]&(1<<BG))) can2 |= 1<<G;
				can = can2;
			}
			if(!can)
				return false;
		}

		return true;
	}

	void apply(vector<int>& P, const string& C)
	{
		for(int i=0; i<C.size(); ++i)
			apply(P[i], C[i]);
	}

	void apply(int& P, char C)
	{
		int Q = 0;
		// N
		if((P&(1<<RG))&&(C!='Z')) Q |= (1<<GB);
		if((P&(1<<RB))&&(C!='Z')) Q |= (1<<BG);
		if((P&(1<<GR))&&(C!='Z')) Q |= (1<<RB);
		if((P&(1<<GB))&&(C!='Z')) Q |= (1<<BR);
		if((P&(1<<BR))&&(C!='Z')) Q |= (1<<RG);
		if((P&(1<<BG))&&(C!='Z')) Q |= (1<<GR);
		// Z
		if((P&(1<<RG))&&(C!='N')) Q |= (1<<BR);
		if((P&(1<<RB))&&(C!='N')) Q |= (1<<GR);
		if((P&(1<<GR))&&(C!='N')) Q |= (1<<BG);
		if((P&(1<<GB))&&(C!='N')) Q |= (1<<RG);
		if((P&(1<<BR))&&(C!='N')) Q |= (1<<GB);
		if((P&(1<<BG))&&(C!='N')) Q |= (1<<RB);

		P = Q;
	}
};

// 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 vector <string>& Expected, const vector <string>& 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(_, ThreeColorability().lexSmallest(cells));}
int main(){

CASE(0)
	string cells_[] = {"Z"};
	  vector <string> cells(cells_, cells_+sizeof(cells_)/sizeof(*cells_)); 
	string __[] = {"Z" };
	  vector <string> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(1)
	string cells_[] = {"??", "?N"};
	  vector <string> cells(cells_, cells_+sizeof(cells_)/sizeof(*cells_)); 
	string __[] = {"NN", "NN" };
	  vector <string> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(2)
	string cells_[] = {"ZZZ", "ZNZ"};
	  vector <string> cells(cells_, cells_+sizeof(cells_)/sizeof(*cells_)); 
	vector <string> _; 
END
CASE(3)
	string cells_[] = {"N?N??NN","??ZN??Z","NN???Z?","ZZZ?Z??","Z???NN?","N?????N","ZZ?N?NN"};
	  vector <string> cells(cells_, cells_+sizeof(cells_)/sizeof(*cells_)); 
	vector <string> _; 
END
CASE(4)
	string cells_[] = {"ZZZZ","ZZZZ","ZZZZ"};
	  vector <string> cells(cells_, cells_+sizeof(cells_)/sizeof(*cells_)); 
	string __[] = {"ZZZZ", "ZZZZ", "ZZZZ" };
	  vector <string> _(__, __+sizeof(__)/sizeof(*__)); 
END
/*
CASE(5)
	string cells_[] = ;
	  vector <string> cells(cells_, cells_+sizeof(cells_)/sizeof(*cells_)); 
	string __[] = ;
	  vector <string> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(6)
	string cells_[] = ;
	  vector <string> cells(cells_, cells_+sizeof(cells_)/sizeof(*cells_)); 
	string __[] = ;
	  vector <string> _(__, __+sizeof(__)/sizeof(*__)); 
END
*/
}
// END CUT HERE