ADDED SRM/TCO15-2B/1A.cpp Index: SRM/TCO15-2B/1A.cpp ================================================================== --- SRM/TCO15-2B/1A.cpp +++ SRM/TCO15-2B/1A.cpp @@ -0,0 +1,86 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +class Bitwisdom { public: + double expectedActions(vector p) + { + double e = 0; + for(int i=1; i +double start_time; string timer() + { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } +template ostream& operator<<(ostream& os, const vector& v) + { os << "{ "; + for(typename vector::const_iterator it=v.begin(); it!=v.end(); ++it) + os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } +void verify_case(const double& Expected, const double& Received) { + bool ok = (abs(Expected - Received) < 1e-9); + 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(_, Bitwisdom().expectedActions(p));} +int main(){ + +CASE(0) + int p_[] = {100, 100, 100}; + vector p(p_, p_+sizeof(p_)/sizeof(*p_)); + double _ = 1.0; +END +CASE(1) + int p_[] = {50, 50}; + vector p(p_, p_+sizeof(p_)/sizeof(*p_)); + double _ = 0.75; +END +CASE(2) + int p_[] = {0, 40, 50, 60}; + vector p(p_, p_+sizeof(p_)/sizeof(*p_)); + double _ = 1.4; +END +CASE(3) + int p_[] = {37, 63, 23, 94, 12}; + vector p(p_, p_+sizeof(p_)/sizeof(*p_)); + double _ = 2.6820475464; +END +/* +CASE(4) + int p_[] = ; + vector p(p_, p_+sizeof(p_)/sizeof(*p_)); + double _ = ; +END +CASE(5) + int p_[] = ; + vector p(p_, p_+sizeof(p_)/sizeof(*p_)); + double _ = ; +END +*/ +} +// END CUT HERE ADDED SRM/TCO15-2B/1B.cpp Index: SRM/TCO15-2B/1B.cpp ================================================================== --- SRM/TCO15-2B/1B.cpp +++ SRM/TCO15-2B/1B.cpp @@ -0,0 +1,204 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +class Balance { public: + string canTransform(vector initial, vector target) + { + auto ti = gen_tree(initial); + auto tt = gen_tree(target); + return tree_eq(ti, tt) ? "Possible" : "Impossible"; + } + + vector> gen_tree(vector s) { + s.push_back(string(s[0].size(), '#')); + s.insert(s.begin(), string(s[0].size(), '#')); + for(auto& si: s) + si = '#'+si+'#'; + + const int H = s.size(), W = s[0].size(); + const int NOTYET = 0x7fffffff; + + vector> tree; + vector> parent_cand(H, vector(W, -1)); + vector> color(H, vector(W, NOTYET)); + for(int y=0; y tonari; + + queue> Q; + color[y][x] = newId; + Q.emplace(y, x); + while(!Q.empty()) { + int yy = Q.front().first; + int xx = Q.front().second; + Q.pop(); + + int dx[] = {-1,+1,0,0}; + int dy[] = {0,0,-1,+1}; + for(int d=0; d<4; ++d) { + int y2 = yy+dy[d]; + int x2 = xx+dx[d]; + if(0<=x2&&x2, int> hashId; + bool tree_eq(const vector>& a, const vector>& b) + { + return hash(a,0) == hash(b,0); + } + + int hash(const vector>& a, int r) + { + vector ch; + for(int v: a[r]) + ch.push_back(hash(a, v)); + sort(ch.begin(), ch.end()); + if(hashId.count(ch)) + return hashId[ch]; + int v = hashId.size(); + return hashId[ch] = v; + } +}; + +// BEGIN CUT HERE +#include +double start_time; string timer() + { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } +template ostream& operator<<(ostream& os, const vector& v) + { os << "{ "; + for(typename vector::const_iterator it=v.begin(); it!=v.end(); ++it) + os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } +void verify_case(const string& Expected, const 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(_, Balance().canTransform(initial, target));} +int main(){ + +CASE(0) + string initial_[] = {"#"}; + vector initial(initial_, initial_+sizeof(initial_)/sizeof(*initial_)); + string target_[] = {"."}; + vector target(target_, target_+sizeof(target_)/sizeof(*target_)); + string _ = "Impossible"; +END +CASE(1) + string initial_[] = {"..", + ".."}; + vector initial(initial_, initial_+sizeof(initial_)/sizeof(*initial_)); + string target_[] = {".#", + "##"}; + vector target(target_, target_+sizeof(target_)/sizeof(*target_)); + string _ = "Possible"; +END +CASE(2) + string initial_[] = {"...", + ".#.", + "..."}; + vector initial(initial_, initial_+sizeof(initial_)/sizeof(*initial_)); + string target_[] = {"###", + "#.#", + "###"}; + vector target(target_, target_+sizeof(target_)/sizeof(*target_)); + string _ = "Impossible"; +END +CASE(3) + string initial_[] = {".#.#.......", + "####.###...", + ".....####..", + "..##.#.##.#", + ".##..#.##.#", + ".#######...", + ".#....###.#", + ".##.#.#....", + "..#...#...#", + "..#####...#", + "..........."}; + vector initial(initial_, initial_+sizeof(initial_)/sizeof(*initial_)); + string target_[] = {"...........", + ".###....#..", + ".#.#..#....", + ".###.......", + ".#####.....", + ".#...#####.", + ".#.#.....#.", + ".#.#####.#.", + ".#.......#.", + ".#########.", + "..........."}; + vector target(target_, target_+sizeof(target_)/sizeof(*target_)); + string _ = "Impossible"; +END +CASE(4) + string initial_[] = {".....", + ".###.", + ".....", + ".###.", + "....."}; + vector initial(initial_, initial_+sizeof(initial_)/sizeof(*initial_)); + string target_[] = {".....", + ".#.#.", + ".#.#.", + ".#.#.", + "....."}; + vector target(target_, target_+sizeof(target_)/sizeof(*target_)); + string _ = "Possible"; +END +/* +CASE(5) + string initial_[] = ; + vector initial(initial_, initial_+sizeof(initial_)/sizeof(*initial_)); + string target_[] = ; + vector target(target_, target_+sizeof(target_)/sizeof(*target_)); + string _ = ; +END +CASE(6) + string initial_[] = ; + vector initial(initial_, initial_+sizeof(initial_)/sizeof(*initial_)); + string target_[] = ; + vector target(target_, target_+sizeof(target_)/sizeof(*target_)); + string _ = ; +END +*/ + +} +// END CUT HERE