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: LL s, N, K; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class PlaneFractal { 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: vector <string> getPattern(int _s, int _N, int _K, int r1, int r2, int c1, int c2) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: s = _s; 4fd800b3a8 2011-02-23 kinaba: N = _N; 4fd800b3a8 2011-02-23 kinaba: K = _K; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: LL Ns=1; for(int i=0; i<s-1; ++i) Ns*=N; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: vector<string> ans(r2-r1+1, string(c2-c1+1, '0')); 4fd800b3a8 2011-02-23 kinaba: for(int r=r1; r<=r2; ++r) 4fd800b3a8 2011-02-23 kinaba: for(int c=c1; c<=c2; ++c) 4fd800b3a8 2011-02-23 kinaba: if( s>0 && black(r,c,Ns) ) 4fd800b3a8 2011-02-23 kinaba: ans[r-r1][c-c1] = '1'; 4fd800b3a8 2011-02-23 kinaba: return ans; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: bool black(int y, int x, LL Ns) const 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: if( Ns == 0 ) 4fd800b3a8 2011-02-23 kinaba: return false; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int Y = y/Ns, X = x/Ns; 4fd800b3a8 2011-02-23 kinaba: y%=Ns, x%=Ns; 4fd800b3a8 2011-02-23 kinaba: if( (Y<(N-K)/2 || N-(N-K)/2<=Y) || (X<(N-K)/2 || N-(N-K)/2<=X) ) 4fd800b3a8 2011-02-23 kinaba: return black(y, x, Ns/N); 4fd800b3a8 2011-02-23 kinaba: return true; 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() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: 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(); } 4fd800b3a8 2011-02-23 kinaba: 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;} 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<int N> struct Case_ { Case_(){start_time=clock();} }; 4fd800b3a8 2011-02-23 kinaba: char Test_(...); 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<0>) { 4fd800b3a8 2011-02-23 kinaba: int s = 1; 4fd800b3a8 2011-02-23 kinaba: int N = 5; 4fd800b3a8 2011-02-23 kinaba: int K = 3; 4fd800b3a8 2011-02-23 kinaba: int r1 = 0; 4fd800b3a8 2011-02-23 kinaba: int r2 = 4; 4fd800b3a8 2011-02-23 kinaba: int c1 = 0; 4fd800b3a8 2011-02-23 kinaba: int c2 = 4; 4fd800b3a8 2011-02-23 kinaba: string RetVal_[] = {"00000", "01110", "01110", "01110", "00000" }; 4fd800b3a8 2011-02-23 kinaba: vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, PlaneFractal().getPattern(s, N, K, r1, r2, c1, c2)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<1>) { 4fd800b3a8 2011-02-23 kinaba: int s = 2; 4fd800b3a8 2011-02-23 kinaba: int N = 3; 4fd800b3a8 2011-02-23 kinaba: int K = 1; 4fd800b3a8 2011-02-23 kinaba: int r1 = 0; 4fd800b3a8 2011-02-23 kinaba: int r2 = 8; 4fd800b3a8 2011-02-23 kinaba: int c1 = 0; 4fd800b3a8 2011-02-23 kinaba: int c2 = 8; 4fd800b3a8 2011-02-23 kinaba: string RetVal_[] = {"000000000", "010010010", "000000000", "000111000", "010111010", "000111000", "000000000", "010010010", "000000000" }; 4fd800b3a8 2011-02-23 kinaba: vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, PlaneFractal().getPattern(s, N, K, r1, r2, c1, c2)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<2>) { 4fd800b3a8 2011-02-23 kinaba: int s = 3; 4fd800b3a8 2011-02-23 kinaba: int N = 3; 4fd800b3a8 2011-02-23 kinaba: int K = 1; 4fd800b3a8 2011-02-23 kinaba: int r1 = 4; 4fd800b3a8 2011-02-23 kinaba: int r2 = 11; 4fd800b3a8 2011-02-23 kinaba: int c1 = 5; 4fd800b3a8 2011-02-23 kinaba: int c2 = 10; 4fd800b3a8 2011-02-23 kinaba: string RetVal_[] = {"101001", "100000", "000000", "001001", "000000", "000011", "001011", "000011" }; 4fd800b3a8 2011-02-23 kinaba: vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, PlaneFractal().getPattern(s, N, K, r1, r2, c1, c2)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<3>) { 4fd800b3a8 2011-02-23 kinaba: int s = 2; 4fd800b3a8 2011-02-23 kinaba: int N = 8; 4fd800b3a8 2011-02-23 kinaba: int K = 4; 4fd800b3a8 2011-02-23 kinaba: int r1 = 56; 4fd800b3a8 2011-02-23 kinaba: int r2 = 61; 4fd800b3a8 2011-02-23 kinaba: int c1 = 33; 4fd800b3a8 2011-02-23 kinaba: int c2 = 56; 4fd800b3a8 2011-02-23 kinaba: string RetVal_[] = {"000000000000000000000000", "000000000000000000000000", "011110000111100001111000", "011110000111100001111000", "011110000111100001111000", "011110000111100001111000" }; 4fd800b3a8 2011-02-23 kinaba: vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, PlaneFractal().getPattern(s, N, K, r1, r2, c1, c2)); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); } 4fd800b3a8 2011-02-23 kinaba: template<> void Run_<-1>() {} 4fd800b3a8 2011-02-23 kinaba: int main() { Run_<0>(); } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE 4fd800b3a8 2011-02-23 kinaba: