Artifact Content
Not logged in

Artifact 1948580a37893b4c32678ff9cda9c29ff8cff660


#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 Balance { public:
	string canTransform(vector <string> initial, vector <string> target)
	{
		auto ti = gen_tree(initial);
		auto tt = gen_tree(target);
		return tree_eq(ti, tt) ? "Possible" : "Impossible";
	}

	vector<vector<int>> gen_tree(vector<string> 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<vector<int>> tree;
		vector<vector<int>> parent_cand(H, vector<int>(W, -1));
		vector<vector<int>> color(H, vector<int>(W, NOTYET));
		for(int y=0; y<H; ++y)
		for(int x=0; x<W; ++x)
		if(color[y][x]==NOTYET) {
			const char ME = s[y][x];
			int newId = tree.size();
			tree.resize(newId+1);

			// BFS
			set<int> tonari;

			queue<pair<int,int>> 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<W&&0<=y2&&y2<H) {
						if(s[y2][x2]==ME&&color[y2][x2]==NOTYET) {
							color[y2][x2] = newId;
							Q.emplace(y2, x2);
						}
						if(s[y2][x2]!=ME&&color[y2][x2]!=NOTYET) {
							tonari.insert(color[y2][x2]);
						}
					}
				}
			}
			if(tonari.size() == 1)
				tree[*tonari.begin()].push_back(newId);
		}
		return tree;
	}

	map<vector<int>, int> hashId;
	bool tree_eq(const vector<vector<int>>& a, const vector<vector<int>>& b)
	{
		return hash(a,0) == hash(b,0);
	}

	int hash(const vector<vector<int>>& a, int r)
	{
		vector<int> 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 <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 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 <string> initial(initial_, initial_+sizeof(initial_)/sizeof(*initial_)); 
	string target_[] = {"."};
	  vector <string> target(target_, target_+sizeof(target_)/sizeof(*target_)); 
	string _ = "Impossible"; 
END
CASE(1)
	string initial_[] = {"..",
 ".."};
	  vector <string> initial(initial_, initial_+sizeof(initial_)/sizeof(*initial_)); 
	string target_[] = {".#",
 "##"};
	  vector <string> target(target_, target_+sizeof(target_)/sizeof(*target_)); 
	string _ = "Possible"; 
END
CASE(2)
	string initial_[] = {"...",
 ".#.",
 "..."};
	  vector <string> initial(initial_, initial_+sizeof(initial_)/sizeof(*initial_)); 
	string target_[] = {"###",
 "#.#",
 "###"};
	  vector <string> target(target_, target_+sizeof(target_)/sizeof(*target_)); 
	string _ = "Impossible"; 
END
CASE(3)
	string initial_[] = {".#.#.......",
 "####.###...",
 ".....####..",
 "..##.#.##.#",
 ".##..#.##.#",
 ".#######...",
 ".#....###.#",
 ".##.#.#....",
 "..#...#...#",
 "..#####...#",
 "..........."};
	  vector <string> initial(initial_, initial_+sizeof(initial_)/sizeof(*initial_)); 
	string target_[] = {"...........",
 ".###....#..",
 ".#.#..#....",
 ".###.......",
 ".#####.....",
 ".#...#####.",
 ".#.#.....#.",
 ".#.#####.#.",
 ".#.......#.",
 ".#########.",
 "..........."};
	  vector <string> target(target_, target_+sizeof(target_)/sizeof(*target_)); 
	string _ = "Impossible"; 
END
CASE(4)
	string initial_[] = {".....",
 ".###.",
 ".....",
 ".###.",
 "....."};
	  vector <string> initial(initial_, initial_+sizeof(initial_)/sizeof(*initial_)); 
	string target_[] = {".....",
 ".#.#.",
 ".#.#.",
 ".#.#.",
 "....."};
	  vector <string> target(target_, target_+sizeof(target_)/sizeof(*target_)); 
	string _ = "Possible"; 
END
/*
CASE(5)
	string initial_[] = ;
	  vector <string> initial(initial_, initial_+sizeof(initial_)/sizeof(*initial_)); 
	string target_[] = ;
	  vector <string> target(target_, target_+sizeof(target_)/sizeof(*target_)); 
	string _ = ; 
END
CASE(6)
	string initial_[] = ;
	  vector <string> initial(initial_, initial_+sizeof(initial_)/sizeof(*initial_)); 
	string target_[] = ;
	  vector <string> target(target_, target_+sizeof(target_)/sizeof(*target_)); 
	string _ = ; 
END
*/

}
// END CUT HERE