/
aoj-1613.test.cpp
93 lines (83 loc) · 2.23 KB
/
aoj-1613.test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1613"
#include "../../template/template.hpp"
//
#include "../../tree/rooted-tree-hash.hpp"
using namespace Nyaan;
// https://kimiyuki.net/writeups/algo-etc-icpc-2016-domestic-f/
#define whole(f, x, ...) \
([&](decltype((x)) y) { return (f)(begin(y), end(y), ##__VA_ARGS__); })(x)
template <typename T, typename X>
auto vectors(T a, X x) {
return vector<T>(x, a);
}
template <typename T, typename X, typename Y, typename... Zs>
auto vectors(T a, X x, Y y, Zs... zs) {
auto cont = vectors(a, y, zs...);
return vector<decltype(cont)>(x, cont);
}
const int dy[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int dx[] = {0, 0, 1, -1, -1, 1, 1, -1};
vector<vector<bool> > load_image() {
int h, w;
cin >> h >> w;
if (h == 0 and w == 0) return vector<vector<bool> >();
vector<vector<bool> > f = vectors(false, h + 2, w + 2);
rep(y, h) rep(x, w) {
char c;
cin >> c;
f[y + 1][x + 1] = c == '#';
}
return f;
}
vector<set<int> > make_tree(vector<vector<bool> > const& f) {
int h = f.size();
int w = f.front().size();
vector<vector<int> > k = vectors(-1, h, w);
vector<set<int> > g;
function<void(int, int, int)> dfs = [&](int y, int x, int cur_k) {
k[y][x] = cur_k;
rep(i, f[y][x] ? 8 : 4) {
int ny = y + dy[i];
int nx = x + dx[i];
if (ny < 0 or h <= ny or nx < 0 or w <= nx) continue;
if (f[ny][nx] == f[y][x]) {
if (k[ny][nx] == -1) {
dfs(ny, nx, cur_k);
}
} else {
if (k[ny][nx] != -1) {
g[k[ny][nx]].insert(k[y][x]);
}
}
}
};
rep(y, h) rep(x, w) if (k[y][x] == -1) {
g.emplace_back();
dfs(y, x, g.size() - 1);
}
return g;
}
using Hash = typename RootedTreeHash<vvi>::Hash;
Hash tree_hash(vector<set<int> > f) {
vvi g(sz(f));
rep(i, sz(f)) {
each(x, f[i]) {
g[i].push_back(x);
g[x].push_back(i);
}
}
RootedTreeHash hash(g);
return hash.hash[0];
}
void Nyaan::solve() {
while (true) {
auto g1 = load_image();
if (g1.empty()) exit(0);
auto g2 = load_image();
auto t1 = make_tree(g1);
auto t2 = make_tree(g2);
auto h1 = tree_hash(t1);
auto h2 = tree_hash(t2);
out(h1 == h2 ? "yes" : "no");
}
}