aae12c19ff 2015-07-23 kinaba: #include <iostream> aae12c19ff 2015-07-23 kinaba: #include <sstream> aae12c19ff 2015-07-23 kinaba: #include <iomanip> aae12c19ff 2015-07-23 kinaba: #include <vector> aae12c19ff 2015-07-23 kinaba: #include <string> aae12c19ff 2015-07-23 kinaba: #include <map> aae12c19ff 2015-07-23 kinaba: #include <set> aae12c19ff 2015-07-23 kinaba: #include <algorithm> aae12c19ff 2015-07-23 kinaba: #include <numeric> aae12c19ff 2015-07-23 kinaba: #include <iterator> aae12c19ff 2015-07-23 kinaba: #include <functional> aae12c19ff 2015-07-23 kinaba: #include <complex> aae12c19ff 2015-07-23 kinaba: #include <queue> aae12c19ff 2015-07-23 kinaba: #include <stack> aae12c19ff 2015-07-23 kinaba: #include <cmath> aae12c19ff 2015-07-23 kinaba: #include <cassert> aae12c19ff 2015-07-23 kinaba: #include <tuple> aae12c19ff 2015-07-23 kinaba: using namespace std; aae12c19ff 2015-07-23 kinaba: typedef long long LL; aae12c19ff 2015-07-23 kinaba: typedef complex<double> CMP; aae12c19ff 2015-07-23 kinaba: aae12c19ff 2015-07-23 kinaba: class Balance { public: aae12c19ff 2015-07-23 kinaba: string canTransform(vector <string> initial, vector <string> target) aae12c19ff 2015-07-23 kinaba: { aae12c19ff 2015-07-23 kinaba: auto ti = gen_tree(initial); aae12c19ff 2015-07-23 kinaba: auto tt = gen_tree(target); aae12c19ff 2015-07-23 kinaba: return tree_eq(ti, tt) ? "Possible" : "Impossible"; aae12c19ff 2015-07-23 kinaba: } aae12c19ff 2015-07-23 kinaba: aae12c19ff 2015-07-23 kinaba: vector<vector<int>> gen_tree(vector<string> s) { aae12c19ff 2015-07-23 kinaba: s.push_back(string(s[0].size(), '#')); aae12c19ff 2015-07-23 kinaba: s.insert(s.begin(), string(s[0].size(), '#')); aae12c19ff 2015-07-23 kinaba: for(auto& si: s) aae12c19ff 2015-07-23 kinaba: si = '#'+si+'#'; aae12c19ff 2015-07-23 kinaba: aae12c19ff 2015-07-23 kinaba: const int H = s.size(), W = s[0].size(); aae12c19ff 2015-07-23 kinaba: const int NOTYET = 0x7fffffff; aae12c19ff 2015-07-23 kinaba: aae12c19ff 2015-07-23 kinaba: vector<vector<int>> tree; aae12c19ff 2015-07-23 kinaba: vector<vector<int>> parent_cand(H, vector<int>(W, -1)); aae12c19ff 2015-07-23 kinaba: vector<vector<int>> color(H, vector<int>(W, NOTYET)); aae12c19ff 2015-07-23 kinaba: for(int y=0; y<H; ++y) aae12c19ff 2015-07-23 kinaba: for(int x=0; x<W; ++x) aae12c19ff 2015-07-23 kinaba: if(color[y][x]==NOTYET) { aae12c19ff 2015-07-23 kinaba: const char ME = s[y][x]; aae12c19ff 2015-07-23 kinaba: int newId = tree.size(); aae12c19ff 2015-07-23 kinaba: tree.resize(newId+1); aae12c19ff 2015-07-23 kinaba: aae12c19ff 2015-07-23 kinaba: // BFS aae12c19ff 2015-07-23 kinaba: set<int> tonari; aae12c19ff 2015-07-23 kinaba: aae12c19ff 2015-07-23 kinaba: queue<pair<int,int>> Q; aae12c19ff 2015-07-23 kinaba: color[y][x] = newId; aae12c19ff 2015-07-23 kinaba: Q.emplace(y, x); aae12c19ff 2015-07-23 kinaba: while(!Q.empty()) { aae12c19ff 2015-07-23 kinaba: int yy = Q.front().first; aae12c19ff 2015-07-23 kinaba: int xx = Q.front().second; aae12c19ff 2015-07-23 kinaba: Q.pop(); aae12c19ff 2015-07-23 kinaba: aae12c19ff 2015-07-23 kinaba: int dx[] = {-1,+1,0,0}; aae12c19ff 2015-07-23 kinaba: int dy[] = {0,0,-1,+1}; aae12c19ff 2015-07-23 kinaba: for(int d=0; d<4; ++d) { aae12c19ff 2015-07-23 kinaba: int y2 = yy+dy[d]; aae12c19ff 2015-07-23 kinaba: int x2 = xx+dx[d]; aae12c19ff 2015-07-23 kinaba: if(0<=x2&&x2<W&&0<=y2&&y2<H) { aae12c19ff 2015-07-23 kinaba: if(s[y2][x2]==ME&&color[y2][x2]==NOTYET) { aae12c19ff 2015-07-23 kinaba: color[y2][x2] = newId; aae12c19ff 2015-07-23 kinaba: Q.emplace(y2, x2); aae12c19ff 2015-07-23 kinaba: } aae12c19ff 2015-07-23 kinaba: if(s[y2][x2]!=ME&&color[y2][x2]!=NOTYET) { aae12c19ff 2015-07-23 kinaba: tonari.insert(color[y2][x2]); aae12c19ff 2015-07-23 kinaba: } aae12c19ff 2015-07-23 kinaba: } aae12c19ff 2015-07-23 kinaba: } aae12c19ff 2015-07-23 kinaba: } aae12c19ff 2015-07-23 kinaba: if(tonari.size() == 1) aae12c19ff 2015-07-23 kinaba: tree[*tonari.begin()].push_back(newId); aae12c19ff 2015-07-23 kinaba: } aae12c19ff 2015-07-23 kinaba: return tree; aae12c19ff 2015-07-23 kinaba: } aae12c19ff 2015-07-23 kinaba: aae12c19ff 2015-07-23 kinaba: map<vector<int>, int> hashId; aae12c19ff 2015-07-23 kinaba: bool tree_eq(const vector<vector<int>>& a, const vector<vector<int>>& b) aae12c19ff 2015-07-23 kinaba: { aae12c19ff 2015-07-23 kinaba: return hash(a,0) == hash(b,0); aae12c19ff 2015-07-23 kinaba: } aae12c19ff 2015-07-23 kinaba: aae12c19ff 2015-07-23 kinaba: int hash(const vector<vector<int>>& a, int r) aae12c19ff 2015-07-23 kinaba: { aae12c19ff 2015-07-23 kinaba: vector<int> ch; aae12c19ff 2015-07-23 kinaba: for(int v: a[r]) aae12c19ff 2015-07-23 kinaba: ch.push_back(hash(a, v)); aae12c19ff 2015-07-23 kinaba: sort(ch.begin(), ch.end()); aae12c19ff 2015-07-23 kinaba: if(hashId.count(ch)) aae12c19ff 2015-07-23 kinaba: return hashId[ch]; aae12c19ff 2015-07-23 kinaba: int v = hashId.size(); aae12c19ff 2015-07-23 kinaba: return hashId[ch] = v; aae12c19ff 2015-07-23 kinaba: } aae12c19ff 2015-07-23 kinaba: }; aae12c19ff 2015-07-23 kinaba: aae12c19ff 2015-07-23 kinaba: // BEGIN CUT HERE aae12c19ff 2015-07-23 kinaba: #include <ctime> aae12c19ff 2015-07-23 kinaba: double start_time; string timer() aae12c19ff 2015-07-23 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } aae12c19ff 2015-07-23 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) aae12c19ff 2015-07-23 kinaba: { os << "{ "; aae12c19ff 2015-07-23 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) aae12c19ff 2015-07-23 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } aae12c19ff 2015-07-23 kinaba: void verify_case(const string& Expected, const string& Received) { aae12c19ff 2015-07-23 kinaba: bool ok = (Expected == Received); aae12c19ff 2015-07-23 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; aae12c19ff 2015-07-23 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } aae12c19ff 2015-07-23 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); aae12c19ff 2015-07-23 kinaba: #define END verify_case(_, Balance().canTransform(initial, target));} aae12c19ff 2015-07-23 kinaba: int main(){ aae12c19ff 2015-07-23 kinaba: aae12c19ff 2015-07-23 kinaba: CASE(0) aae12c19ff 2015-07-23 kinaba: string initial_[] = {"#"}; aae12c19ff 2015-07-23 kinaba: vector <string> initial(initial_, initial_+sizeof(initial_)/sizeof(*initial_)); aae12c19ff 2015-07-23 kinaba: string target_[] = {"."}; aae12c19ff 2015-07-23 kinaba: vector <string> target(target_, target_+sizeof(target_)/sizeof(*target_)); aae12c19ff 2015-07-23 kinaba: string _ = "Impossible"; aae12c19ff 2015-07-23 kinaba: END aae12c19ff 2015-07-23 kinaba: CASE(1) aae12c19ff 2015-07-23 kinaba: string initial_[] = {"..", aae12c19ff 2015-07-23 kinaba: ".."}; aae12c19ff 2015-07-23 kinaba: vector <string> initial(initial_, initial_+sizeof(initial_)/sizeof(*initial_)); aae12c19ff 2015-07-23 kinaba: string target_[] = {".#", aae12c19ff 2015-07-23 kinaba: "##"}; aae12c19ff 2015-07-23 kinaba: vector <string> target(target_, target_+sizeof(target_)/sizeof(*target_)); aae12c19ff 2015-07-23 kinaba: string _ = "Possible"; aae12c19ff 2015-07-23 kinaba: END aae12c19ff 2015-07-23 kinaba: CASE(2) aae12c19ff 2015-07-23 kinaba: string initial_[] = {"...", aae12c19ff 2015-07-23 kinaba: ".#.", aae12c19ff 2015-07-23 kinaba: "..."}; aae12c19ff 2015-07-23 kinaba: vector <string> initial(initial_, initial_+sizeof(initial_)/sizeof(*initial_)); aae12c19ff 2015-07-23 kinaba: string target_[] = {"###", aae12c19ff 2015-07-23 kinaba: "#.#", aae12c19ff 2015-07-23 kinaba: "###"}; aae12c19ff 2015-07-23 kinaba: vector <string> target(target_, target_+sizeof(target_)/sizeof(*target_)); aae12c19ff 2015-07-23 kinaba: string _ = "Impossible"; aae12c19ff 2015-07-23 kinaba: END aae12c19ff 2015-07-23 kinaba: CASE(3) aae12c19ff 2015-07-23 kinaba: string initial_[] = {".#.#.......", aae12c19ff 2015-07-23 kinaba: "####.###...", aae12c19ff 2015-07-23 kinaba: ".....####..", aae12c19ff 2015-07-23 kinaba: "..##.#.##.#", aae12c19ff 2015-07-23 kinaba: ".##..#.##.#", aae12c19ff 2015-07-23 kinaba: ".#######...", aae12c19ff 2015-07-23 kinaba: ".#....###.#", aae12c19ff 2015-07-23 kinaba: ".##.#.#....", aae12c19ff 2015-07-23 kinaba: "..#...#...#", aae12c19ff 2015-07-23 kinaba: "..#####...#", aae12c19ff 2015-07-23 kinaba: "..........."}; aae12c19ff 2015-07-23 kinaba: vector <string> initial(initial_, initial_+sizeof(initial_)/sizeof(*initial_)); aae12c19ff 2015-07-23 kinaba: string target_[] = {"...........", aae12c19ff 2015-07-23 kinaba: ".###....#..", aae12c19ff 2015-07-23 kinaba: ".#.#..#....", aae12c19ff 2015-07-23 kinaba: ".###.......", aae12c19ff 2015-07-23 kinaba: ".#####.....", aae12c19ff 2015-07-23 kinaba: ".#...#####.", aae12c19ff 2015-07-23 kinaba: ".#.#.....#.", aae12c19ff 2015-07-23 kinaba: ".#.#####.#.", aae12c19ff 2015-07-23 kinaba: ".#.......#.", aae12c19ff 2015-07-23 kinaba: ".#########.", aae12c19ff 2015-07-23 kinaba: "..........."}; aae12c19ff 2015-07-23 kinaba: vector <string> target(target_, target_+sizeof(target_)/sizeof(*target_)); aae12c19ff 2015-07-23 kinaba: string _ = "Impossible"; aae12c19ff 2015-07-23 kinaba: END aae12c19ff 2015-07-23 kinaba: CASE(4) aae12c19ff 2015-07-23 kinaba: string initial_[] = {".....", aae12c19ff 2015-07-23 kinaba: ".###.", aae12c19ff 2015-07-23 kinaba: ".....", aae12c19ff 2015-07-23 kinaba: ".###.", aae12c19ff 2015-07-23 kinaba: "....."}; aae12c19ff 2015-07-23 kinaba: vector <string> initial(initial_, initial_+sizeof(initial_)/sizeof(*initial_)); aae12c19ff 2015-07-23 kinaba: string target_[] = {".....", aae12c19ff 2015-07-23 kinaba: ".#.#.", aae12c19ff 2015-07-23 kinaba: ".#.#.", aae12c19ff 2015-07-23 kinaba: ".#.#.", aae12c19ff 2015-07-23 kinaba: "....."}; aae12c19ff 2015-07-23 kinaba: vector <string> target(target_, target_+sizeof(target_)/sizeof(*target_)); aae12c19ff 2015-07-23 kinaba: string _ = "Possible"; aae12c19ff 2015-07-23 kinaba: END aae12c19ff 2015-07-23 kinaba: /* aae12c19ff 2015-07-23 kinaba: CASE(5) aae12c19ff 2015-07-23 kinaba: string initial_[] = ; aae12c19ff 2015-07-23 kinaba: vector <string> initial(initial_, initial_+sizeof(initial_)/sizeof(*initial_)); aae12c19ff 2015-07-23 kinaba: string target_[] = ; aae12c19ff 2015-07-23 kinaba: vector <string> target(target_, target_+sizeof(target_)/sizeof(*target_)); aae12c19ff 2015-07-23 kinaba: string _ = ; aae12c19ff 2015-07-23 kinaba: END aae12c19ff 2015-07-23 kinaba: CASE(6) aae12c19ff 2015-07-23 kinaba: string initial_[] = ; aae12c19ff 2015-07-23 kinaba: vector <string> initial(initial_, initial_+sizeof(initial_)/sizeof(*initial_)); aae12c19ff 2015-07-23 kinaba: string target_[] = ; aae12c19ff 2015-07-23 kinaba: vector <string> target(target_, target_+sizeof(target_)/sizeof(*target_)); aae12c19ff 2015-07-23 kinaba: string _ = ; aae12c19ff 2015-07-23 kinaba: END aae12c19ff 2015-07-23 kinaba: */ aae12c19ff 2015-07-23 kinaba: aae12c19ff 2015-07-23 kinaba: } aae12c19ff 2015-07-23 kinaba: // END CUT HERE