143923277f 2014-11-03 kinaba: #include <iostream> 143923277f 2014-11-03 kinaba: #include <sstream> 143923277f 2014-11-03 kinaba: #include <iomanip> 143923277f 2014-11-03 kinaba: #include <vector> 143923277f 2014-11-03 kinaba: #include <string> 143923277f 2014-11-03 kinaba: #include <map> 143923277f 2014-11-03 kinaba: #include <set> 143923277f 2014-11-03 kinaba: #include <algorithm> 143923277f 2014-11-03 kinaba: #include <numeric> 143923277f 2014-11-03 kinaba: #include <iterator> 143923277f 2014-11-03 kinaba: #include <functional> 143923277f 2014-11-03 kinaba: #include <complex> 143923277f 2014-11-03 kinaba: #include <queue> 143923277f 2014-11-03 kinaba: #include <stack> 143923277f 2014-11-03 kinaba: #include <cmath> 143923277f 2014-11-03 kinaba: #include <cassert> 143923277f 2014-11-03 kinaba: #include <tuple> 143923277f 2014-11-03 kinaba: using namespace std; 143923277f 2014-11-03 kinaba: typedef long long LL; 143923277f 2014-11-03 kinaba: typedef complex<double> CMP; 143923277f 2014-11-03 kinaba: 143923277f 2014-11-03 kinaba: struct UnionFind 143923277f 2014-11-03 kinaba: { 143923277f 2014-11-03 kinaba: vector<int> uf, sz; 143923277f 2014-11-03 kinaba: int nc; 143923277f 2014-11-03 kinaba: 143923277f 2014-11-03 kinaba: UnionFind(int N): uf(N), sz(N,1), nc(N) 143923277f 2014-11-03 kinaba: { for(int i=0; i<N; ++i) uf[i] = i; } 143923277f 2014-11-03 kinaba: int size() 143923277f 2014-11-03 kinaba: { return nc; } 143923277f 2014-11-03 kinaba: int size(int a) 143923277f 2014-11-03 kinaba: { return sz[Find(a)]; } 143923277f 2014-11-03 kinaba: int Find(int a) 143923277f 2014-11-03 kinaba: { return uf[a]==a ? a : uf[a]=Find(uf[a]); } 143923277f 2014-11-03 kinaba: bool Union(int a, int b) 143923277f 2014-11-03 kinaba: { 143923277f 2014-11-03 kinaba: a = Find(a); 143923277f 2014-11-03 kinaba: b = Find(b); 143923277f 2014-11-03 kinaba: if( a != b ) 143923277f 2014-11-03 kinaba: { 143923277f 2014-11-03 kinaba: if( sz[a] >= sz[b] ) swap(a, b); 143923277f 2014-11-03 kinaba: uf[a] = b; 143923277f 2014-11-03 kinaba: sz[b] += sz[a]; 143923277f 2014-11-03 kinaba: --nc; 143923277f 2014-11-03 kinaba: } 143923277f 2014-11-03 kinaba: return (a!=b); 143923277f 2014-11-03 kinaba: } 143923277f 2014-11-03 kinaba: }; 143923277f 2014-11-03 kinaba: 143923277f 2014-11-03 kinaba: class ShadowSculpture { public: 143923277f 2014-11-03 kinaba: string possible(vector <string> XY, vector <string> YZ, vector <string> ZX) 143923277f 2014-11-03 kinaba: { 143923277f 2014-11-03 kinaba: const int N = XY.size(); 143923277f 2014-11-03 kinaba: 143923277f 2014-11-03 kinaba: // Init 143923277f 2014-11-03 kinaba: bool XYZ[10][10][10]; 143923277f 2014-11-03 kinaba: for(int x=0; x<N; ++x) 143923277f 2014-11-03 kinaba: for(int y=0; y<N; ++y) 143923277f 2014-11-03 kinaba: for(int z=0; z<N; ++z) 143923277f 2014-11-03 kinaba: XYZ[x][y][z] = true; 143923277f 2014-11-03 kinaba: 143923277f 2014-11-03 kinaba: // Grave 143923277f 2014-11-03 kinaba: for(int x=0; x<N; ++x) 143923277f 2014-11-03 kinaba: for(int y=0; y<N; ++y) 143923277f 2014-11-03 kinaba: for(int z=0; z<N; ++z) 143923277f 2014-11-03 kinaba: if(XY[x][y] == 'N' || YZ[y][z] == 'N' || ZX[z][x] == 'N') 143923277f 2014-11-03 kinaba: XYZ[x][y][z] = false; 143923277f 2014-11-03 kinaba: 143923277f 2014-11-03 kinaba: // Sanity check 143923277f 2014-11-03 kinaba: for(int x=0; x<N; ++x) 143923277f 2014-11-03 kinaba: for(int y=0; y<N; ++y) { 143923277f 2014-11-03 kinaba: bool a=false,b=false,c=false; 143923277f 2014-11-03 kinaba: for(int z=0; z<N; ++z) { 143923277f 2014-11-03 kinaba: a |= XYZ[x][y][z]; 143923277f 2014-11-03 kinaba: b |= XYZ[z][x][y]; 143923277f 2014-11-03 kinaba: c |= XYZ[y][z][x]; 143923277f 2014-11-03 kinaba: } 143923277f 2014-11-03 kinaba: if(XY[x][y]=='Y'&&!a) return "Impossible"; 143923277f 2014-11-03 kinaba: if(YZ[x][y]=='Y'&&!b) return "Impossible"; 143923277f 2014-11-03 kinaba: if(ZX[x][y]=='Y'&&!c) return "Impossible"; 143923277f 2014-11-03 kinaba: } 143923277f 2014-11-03 kinaba: 143923277f 2014-11-03 kinaba: // Judge 143923277f 2014-11-03 kinaba: return is_connected(XYZ, N) ? "Possible" : "Impossible"; 143923277f 2014-11-03 kinaba: } 143923277f 2014-11-03 kinaba: 143923277f 2014-11-03 kinaba: bool is_connected(bool XYZ[10][10][10], int N) 143923277f 2014-11-03 kinaba: { 143923277f 2014-11-03 kinaba: UnionFind uf(N*N*N); 143923277f 2014-11-03 kinaba: function<int(int,int,int)> id = [&](int x, int y, int z) { 143923277f 2014-11-03 kinaba: return (x*N+y)*N+z; 143923277f 2014-11-03 kinaba: }; 143923277f 2014-11-03 kinaba: 143923277f 2014-11-03 kinaba: const int dx[] = {-1,+1,0,0,0,0}; 143923277f 2014-11-03 kinaba: const int dy[] = {0,0,-1,+1,0,0}; 143923277f 2014-11-03 kinaba: const int dz[] = {0,0,0,0,-1,+1}; 143923277f 2014-11-03 kinaba: int cnt = 0; 143923277f 2014-11-03 kinaba: for(int x=0; x<N; ++x) 143923277f 2014-11-03 kinaba: for(int y=0; y<N; ++y) 143923277f 2014-11-03 kinaba: for(int z=0; z<N; ++z) if(XYZ[x][y][z]) { 143923277f 2014-11-03 kinaba: ++cnt; 143923277f 2014-11-03 kinaba: for(int d=0; d<6; ++d) { 143923277f 2014-11-03 kinaba: int xx=x+dx[d], yy=y+dy[d], zz=z+dz[d]; 143923277f 2014-11-03 kinaba: if(0<=xx&&xx<N&&0<=yy&&yy<N&&0<=zz&&zz<N&&XYZ[xx][yy][zz]) 143923277f 2014-11-03 kinaba: uf.Union(id(x,y,z), id(xx,yy,zz)); 143923277f 2014-11-03 kinaba: } 143923277f 2014-11-03 kinaba: } 143923277f 2014-11-03 kinaba: 143923277f 2014-11-03 kinaba: for(int x=0; x<N; ++x) 143923277f 2014-11-03 kinaba: for(int y=0; y<N; ++y) 143923277f 2014-11-03 kinaba: for(int z=0; z<N; ++z) if(XYZ[x][y][z] && uf.size(id(x,y,z))!=cnt) 143923277f 2014-11-03 kinaba: return false; 143923277f 2014-11-03 kinaba: return true; 143923277f 2014-11-03 kinaba: } 143923277f 2014-11-03 kinaba: }; 143923277f 2014-11-03 kinaba: 143923277f 2014-11-03 kinaba: // BEGIN CUT HERE 143923277f 2014-11-03 kinaba: #include <ctime> 143923277f 2014-11-03 kinaba: double start_time; string timer() 143923277f 2014-11-03 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 143923277f 2014-11-03 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 143923277f 2014-11-03 kinaba: { os << "{ "; 143923277f 2014-11-03 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 143923277f 2014-11-03 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 143923277f 2014-11-03 kinaba: void verify_case(const string& Expected, const string& Received) { 143923277f 2014-11-03 kinaba: bool ok = (Expected == Received); 143923277f 2014-11-03 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 143923277f 2014-11-03 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 143923277f 2014-11-03 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 143923277f 2014-11-03 kinaba: #define END verify_case(_, ShadowSculpture().possible(XY, YZ, ZX));} 143923277f 2014-11-03 kinaba: int main(){ 143923277f 2014-11-03 kinaba: 143923277f 2014-11-03 kinaba: CASE(0) 143923277f 2014-11-03 kinaba: string XY_[] = {"YN","NN"}; 143923277f 2014-11-03 kinaba: vector <string> XY(XY_, XY_+sizeof(XY_)/sizeof(*XY_)); 143923277f 2014-11-03 kinaba: string YZ_[] = {"YN","NN"}; 143923277f 2014-11-03 kinaba: vector <string> YZ(YZ_, YZ_+sizeof(YZ_)/sizeof(*YZ_)); 143923277f 2014-11-03 kinaba: string ZX_[] = {"YN","NN"}; 143923277f 2014-11-03 kinaba: vector <string> ZX(ZX_, ZX_+sizeof(ZX_)/sizeof(*ZX_)); 143923277f 2014-11-03 kinaba: string _ = "Possible"; 143923277f 2014-11-03 kinaba: END 143923277f 2014-11-03 kinaba: CASE(1) 143923277f 2014-11-03 kinaba: string XY_[] = {"YN","NY"}; 143923277f 2014-11-03 kinaba: vector <string> XY(XY_, XY_+sizeof(XY_)/sizeof(*XY_)); 143923277f 2014-11-03 kinaba: string YZ_[] = {"YN","NY"}; 143923277f 2014-11-03 kinaba: vector <string> YZ(YZ_, YZ_+sizeof(YZ_)/sizeof(*YZ_)); 143923277f 2014-11-03 kinaba: string ZX_[] = {"YN","NY"}; 143923277f 2014-11-03 kinaba: vector <string> ZX(ZX_, ZX_+sizeof(ZX_)/sizeof(*ZX_)); 143923277f 2014-11-03 kinaba: string _ = "Impossible"; 143923277f 2014-11-03 kinaba: END 143923277f 2014-11-03 kinaba: CASE(2) 143923277f 2014-11-03 kinaba: string XY_[] = {"YYY","YNY","YYY"}; 143923277f 2014-11-03 kinaba: vector <string> XY(XY_, XY_+sizeof(XY_)/sizeof(*XY_)); 143923277f 2014-11-03 kinaba: string YZ_[] = {"YYY","YNY","YYY"}; 143923277f 2014-11-03 kinaba: vector <string> YZ(YZ_, YZ_+sizeof(YZ_)/sizeof(*YZ_)); 143923277f 2014-11-03 kinaba: string ZX_[] = {"YYY","YNY","YYY"}; 143923277f 2014-11-03 kinaba: vector <string> ZX(ZX_, ZX_+sizeof(ZX_)/sizeof(*ZX_)); 143923277f 2014-11-03 kinaba: string _ = "Possible"; 143923277f 2014-11-03 kinaba: END 143923277f 2014-11-03 kinaba: CASE(3) 143923277f 2014-11-03 kinaba: string XY_[] = {"YYY","YNY","YYY"}; 143923277f 2014-11-03 kinaba: vector <string> XY(XY_, XY_+sizeof(XY_)/sizeof(*XY_)); 143923277f 2014-11-03 kinaba: string YZ_[] = {"NYY","YNY","YYY"}; 143923277f 2014-11-03 kinaba: vector <string> YZ(YZ_, YZ_+sizeof(YZ_)/sizeof(*YZ_)); 143923277f 2014-11-03 kinaba: string ZX_[] = {"YYY","YNY","YYN"}; 143923277f 2014-11-03 kinaba: vector <string> ZX(ZX_, ZX_+sizeof(ZX_)/sizeof(*ZX_)); 143923277f 2014-11-03 kinaba: string _ = "Impossible"; 143923277f 2014-11-03 kinaba: END 143923277f 2014-11-03 kinaba: CASE(4) 143923277f 2014-11-03 kinaba: string XY_[] = {"N"}; 143923277f 2014-11-03 kinaba: vector <string> XY(XY_, XY_+sizeof(XY_)/sizeof(*XY_)); 143923277f 2014-11-03 kinaba: string YZ_[] = {"N"}; 143923277f 2014-11-03 kinaba: vector <string> YZ(YZ_, YZ_+sizeof(YZ_)/sizeof(*YZ_)); 143923277f 2014-11-03 kinaba: string ZX_[] = {"N"}; 143923277f 2014-11-03 kinaba: vector <string> ZX(ZX_, ZX_+sizeof(ZX_)/sizeof(*ZX_)); 143923277f 2014-11-03 kinaba: string _ = "Possible"; 143923277f 2014-11-03 kinaba: END 143923277f 2014-11-03 kinaba: /* 143923277f 2014-11-03 kinaba: CASE(5) 143923277f 2014-11-03 kinaba: string XY_[] = ; 143923277f 2014-11-03 kinaba: vector <string> XY(XY_, XY_+sizeof(XY_)/sizeof(*XY_)); 143923277f 2014-11-03 kinaba: string YZ_[] = ; 143923277f 2014-11-03 kinaba: vector <string> YZ(YZ_, YZ_+sizeof(YZ_)/sizeof(*YZ_)); 143923277f 2014-11-03 kinaba: string ZX_[] = ; 143923277f 2014-11-03 kinaba: vector <string> ZX(ZX_, ZX_+sizeof(ZX_)/sizeof(*ZX_)); 143923277f 2014-11-03 kinaba: string _ = ; 143923277f 2014-11-03 kinaba: END 143923277f 2014-11-03 kinaba: CASE(6) 143923277f 2014-11-03 kinaba: string XY_[] = ; 143923277f 2014-11-03 kinaba: vector <string> XY(XY_, XY_+sizeof(XY_)/sizeof(*XY_)); 143923277f 2014-11-03 kinaba: string YZ_[] = ; 143923277f 2014-11-03 kinaba: vector <string> YZ(YZ_, YZ_+sizeof(YZ_)/sizeof(*YZ_)); 143923277f 2014-11-03 kinaba: string ZX_[] = ; 143923277f 2014-11-03 kinaba: vector <string> ZX(ZX_, ZX_+sizeof(ZX_)/sizeof(*ZX_)); 143923277f 2014-11-03 kinaba: string _ = ; 143923277f 2014-11-03 kinaba: END 143923277f 2014-11-03 kinaba: */ 143923277f 2014-11-03 kinaba: } 143923277f 2014-11-03 kinaba: // END CUT HERE